Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions sample2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
authenticationOptions.AuthScheme = CookieAuthenticationDefaults.AuthenticationScheme;
authenticationOptions.SilkierQuartzClaim = "Silkier";
authenticationOptions.SilkierQuartzClaimValue = "Quartz";
authenticationOptions.UserName = "admin";
authenticationOptions.UserPassword = "password";
authenticationOptions.AccessRequirement = SilkierQuartzAuthenticationOptions.SimpleAccessRequirement.AllowOnlyUsersWithClaim;
}
#else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using System;
using Microsoft.AspNetCore.Authentication.Cookies;
using SilkierQuartz.Authorization;

namespace SilkierQuartz
Expand All @@ -19,8 +20,16 @@ public enum SimpleAccessRequirement
}

public const string AuthorizationPolicyName = "SilkierQuartz";
public string UserName { get; set; } = "admin";
public string UserPassword { get; set; } = "password";

public const string DefaultUserName = "admin";
public const string DefaultPassword = "password";

public Func<string, string, bool> Authenticate = (userName, password) =>
{
return
string.Compare(userName, SilkierQuartzAuthenticationOptions.DefaultUserName, StringComparison.InvariantCulture) == 0 &&
string.Compare(password, SilkierQuartzAuthenticationOptions.DefaultPassword, StringComparison.InvariantCulture) == 0;
};

/// <summary>
/// Sets the authentication scheme for the SilkierQuartz authentication signin.
Expand Down
31 changes: 13 additions & 18 deletions src/SilkierQuartz/Controllers/AuthenticateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@

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

if (string.IsNullOrEmpty(authenticationOptions.UserName) ||
string.IsNullOrEmpty(authenticationOptions.UserPassword))
if (authenticationOptions.Authenticate == null)
{
foreach (var userClaim in HttpContext.User.Claims)
{
Expand All @@ -42,8 +41,7 @@
!HttpContext.User.HasClaim(authenticationOptions.SilkierQuartzClaim,
authenticationOptions.SilkierQuartzClaimValue))
{
await SignIn(false);

await SignIn(false, SilkierQuartzAuthenticationOptions.DefaultUserName, SilkierQuartzAuthenticationOptions.DefaultPassword);
return RedirectToAction(nameof(SchedulerController.Index), nameof(Scheduler));
}
else
Expand All @@ -70,40 +68,37 @@
public async Task<IActionResult> Login([FromForm] AuthenticateViewModel request)
{
var form = HttpContext.Request.Form;

if (string.Compare(request.UserName, authenticationOptions.UserName,
StringComparison.InvariantCulture) != 0 ||
string.Compare(request.Password, authenticationOptions.UserPassword,
StringComparison.InvariantCulture) != 0)
if (!authenticationOptions.Authenticate(request.UserName, request.Password))
{
request.IsLoginError = true;
return View(request);
}

await SignIn(request.IsPersist);

return RedirectToAction(nameof(SchedulerController.Index), nameof(Scheduler));
else
{
await SignIn(request.IsPersist, request.UserName, request.Password);
return RedirectToAction(nameof(SchedulerController.Index), nameof(Scheduler));
}
}

[HttpGet]
[Authorize(Policy = SilkierQuartzAuthenticationOptions.AuthorizationPolicyName)]

Check warning on line 84 in src/SilkierQuartz/Controllers/AuthenticateController.cs

View workflow job for this annotation

GitHub Actions / build

This

Check warning on line 84 in src/SilkierQuartz/Controllers/AuthenticateController.cs

View workflow job for this annotation

GitHub Actions / build

This
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync(authenticationOptions.AuthScheme);
return RedirectToAction(nameof(Login));
}

private async Task SignIn(bool isPersistentSignIn)
private async Task SignIn(bool isPersistentSignIn, string userName, string password)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, string.IsNullOrEmpty(authenticationOptions.UserName)
new Claim(ClaimTypes.NameIdentifier, string.IsNullOrEmpty(userName)
? "SilkierQuartzAdmin"
: authenticationOptions.UserName ),
: SilkierQuartzAuthenticationOptions.DefaultUserName),

new Claim(ClaimTypes.Name, string.IsNullOrEmpty(authenticationOptions.UserPassword)
new Claim(ClaimTypes.Name, string.IsNullOrEmpty(password)
? "SilkierQuartzPassword"
: authenticationOptions.UserPassword),
: SilkierQuartzAuthenticationOptions.DefaultPassword),

new Claim(authenticationOptions.SilkierQuartzClaim, authenticationOptions.SilkierQuartzClaimValue)
};
Expand Down