Skip to content

Commit 3c52d6f

Browse files
authored
Merge pull request #160 from AT-WH/master
Extending Authentication mechanism
2 parents e7c68db + e1301b8 commit 3c52d6f

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

sample2/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
authenticationOptions.AuthScheme = CookieAuthenticationDefaults.AuthenticationScheme;
4141
authenticationOptions.SilkierQuartzClaim = "Silkier";
4242
authenticationOptions.SilkierQuartzClaimValue = "Quartz";
43-
authenticationOptions.UserName = "admin";
44-
authenticationOptions.UserPassword = "password";
4543
authenticationOptions.AccessRequirement = SilkierQuartzAuthenticationOptions.SimpleAccessRequirement.AllowOnlyUsersWithClaim;
4644
}
4745
#else

src/SilkierQuartz/Authorization/SilkierQuartzAuthenticationOptions.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Authentication.Cookies;
1+
using System;
2+
using Microsoft.AspNetCore.Authentication.Cookies;
23
using SilkierQuartz.Authorization;
34

45
namespace SilkierQuartz
@@ -19,8 +20,16 @@ public enum SimpleAccessRequirement
1920
}
2021

2122
public const string AuthorizationPolicyName = "SilkierQuartz";
22-
public string UserName { get; set; } = "admin";
23-
public string UserPassword { get; set; } = "password";
23+
24+
public const string DefaultUserName = "admin";
25+
public const string DefaultPassword = "password";
26+
27+
public Func<string, string, bool> Authenticate = (userName, password) =>
28+
{
29+
return
30+
string.Compare(userName, SilkierQuartzAuthenticationOptions.DefaultUserName, StringComparison.InvariantCulture) == 0 &&
31+
string.Compare(password, SilkierQuartzAuthenticationOptions.DefaultPassword, StringComparison.InvariantCulture) == 0;
32+
};
2433

2534
/// <summary>
2635
/// Sets the authentication scheme for the SilkierQuartz authentication signin.

src/SilkierQuartz/Controllers/AuthenticateController.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public async Task<IActionResult> Login([FromServices] IAuthenticationSchemeProvi
3030

3131
var silkierScheme = await schemes.GetSchemeAsync(authenticationOptions.AuthScheme);
3232

33-
if (string.IsNullOrEmpty(authenticationOptions.UserName) ||
34-
string.IsNullOrEmpty(authenticationOptions.UserPassword))
33+
if (authenticationOptions.Authenticate == null)
3534
{
3635
foreach (var userClaim in HttpContext.User.Claims)
3736
{
@@ -42,8 +41,7 @@ public async Task<IActionResult> Login([FromServices] IAuthenticationSchemeProvi
4241
!HttpContext.User.HasClaim(authenticationOptions.SilkierQuartzClaim,
4342
authenticationOptions.SilkierQuartzClaimValue))
4443
{
45-
await SignIn(false);
46-
44+
await SignIn(false, SilkierQuartzAuthenticationOptions.DefaultUserName, SilkierQuartzAuthenticationOptions.DefaultPassword);
4745
return RedirectToAction(nameof(SchedulerController.Index), nameof(Scheduler));
4846
}
4947
else
@@ -70,19 +68,16 @@ public async Task<IActionResult> Login([FromServices] IAuthenticationSchemeProvi
7068
public async Task<IActionResult> Login([FromForm] AuthenticateViewModel request)
7169
{
7270
var form = HttpContext.Request.Form;
73-
74-
if (string.Compare(request.UserName, authenticationOptions.UserName,
75-
StringComparison.InvariantCulture) != 0 ||
76-
string.Compare(request.Password, authenticationOptions.UserPassword,
77-
StringComparison.InvariantCulture) != 0)
71+
if (!authenticationOptions.Authenticate(request.UserName, request.Password))
7872
{
7973
request.IsLoginError = true;
8074
return View(request);
8175
}
82-
83-
await SignIn(request.IsPersist);
84-
85-
return RedirectToAction(nameof(SchedulerController.Index), nameof(Scheduler));
76+
else
77+
{
78+
await SignIn(request.IsPersist, request.UserName, request.Password);
79+
return RedirectToAction(nameof(SchedulerController.Index), nameof(Scheduler));
80+
}
8681
}
8782

8883
[HttpGet]
@@ -93,17 +88,17 @@ public async Task<IActionResult> Logout()
9388
return RedirectToAction(nameof(Login));
9489
}
9590

96-
private async Task SignIn(bool isPersistentSignIn)
91+
private async Task SignIn(bool isPersistentSignIn, string userName, string password)
9792
{
9893
var claims = new List<Claim>
9994
{
100-
new Claim(ClaimTypes.NameIdentifier, string.IsNullOrEmpty(authenticationOptions.UserName)
95+
new Claim(ClaimTypes.NameIdentifier, string.IsNullOrEmpty(userName)
10196
? "SilkierQuartzAdmin"
102-
: authenticationOptions.UserName ),
97+
: SilkierQuartzAuthenticationOptions.DefaultUserName),
10398

104-
new Claim(ClaimTypes.Name, string.IsNullOrEmpty(authenticationOptions.UserPassword)
99+
new Claim(ClaimTypes.Name, string.IsNullOrEmpty(password)
105100
? "SilkierQuartzPassword"
106-
: authenticationOptions.UserPassword),
101+
: SilkierQuartzAuthenticationOptions.DefaultPassword),
107102

108103
new Claim(authenticationOptions.SilkierQuartzClaim, authenticationOptions.SilkierQuartzClaimValue)
109104
};

0 commit comments

Comments
 (0)