Skip to content

Instantly share code, notes, and snippets.

@michaeltnguyen
Created November 27, 2018 19:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeltnguyen/718f801fba2fdf370e2e8c5a5e763e08 to your computer and use it in GitHub Desktop.
Save michaeltnguyen/718f801fba2fdf370e2e8c5a5e763e08 to your computer and use it in GitHub Desktop.
public class LocalAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
/// <summary>
/// The default user id, injected into the claims for all requests.
/// </summary>
public static readonly Guid UserId = Guid.Parse("687d7c63-9a15-4faf-af5a-140782baa24d");
/// <summary>
/// The name of the authorizaton scheme that this handler will respond to.
/// </summary>
public const string AuthScheme = "LocalAuth";
private readonly Claim DefaultUserIdClaim = new Claim(
UserIdClaimType, UserId.ToString());
public LocalAuthenticationHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger,
UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}
/// <summary>
/// Marks all authentication requests as successful, and injects the
/// default company id into the user claims.
/// </summary>
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
var authenticationTicket = new AuthenticationTicket(
new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { DefaultUserIdClaim }, AuthScheme)),
new AuthenticationProperties(),
AuthScheme);
return Task.FromResult(AuthenticateResult.Success(authenticationTicket));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment