Skip to content

Instantly share code, notes, and snippets.

@esergueev
Created August 2, 2016 06:23
Show Gist options
  • Save esergueev/82eb47dfb731bba39e0ae9a696477fae to your computer and use it in GitHub Desktop.
Save esergueev/82eb47dfb731bba39e0ae9a696477fae to your computer and use it in GitHub Desktop.
Code review
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
ExpireTimeSpan = TimeSpan.FromDays(30),
SlidingExpiration = true,
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = async cookieCtx =>
{
var validator = DependencyResolver.Current.GetService<IAuthenticationSessionValidator>();
var isValid = false;
if (cookieCtx.Properties.IssuedUtc.HasValue)
{
cookieCtx.Identity.AddClaim(new Claim(Constants.ClaimTypes.AuthenticationTime,
cookieCtx.Properties.IssuedUtc.Value.ToEpochTime().ToString()));
isValid =
await
validator.IsAuthenticationSessionValidAsync(new ClaimsPrincipal(cookieCtx.Identity));
}
if (isValid == false)
{
cookieCtx.RejectIdentity();
}
}
}
});
public class DataEntryAuthenticationSessionValidator : IAuthenticationAppSessionValidator
{
private readonly IMediator _mediator;
public DataEntryAuthenticationSessionValidator(IMediator mediator)
{
_mediator = mediator;
}
public Task<bool> IsAuthenticationSessionValidAsync(CookieValidateIdentityContext ctx)
{
var date = _mediator.Send(new ReadRecordQuery<PractitionerRecord>(ctx.Identity.GetSubjectId())).Updated;
return
Task.FromResult(ctx.Properties.IssuedUtc.HasValue &&
DateTimeOffset.Compare(new DateTimeOffset(date), ctx.Properties.IssuedUtc.Value) < 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment