在文章中,他们在选项中引用了CookieValidatePrincipalContext
代表OnValidatePrincipal
的CookieAuthenticationEvents
。
你必须像这样在app.UseCookieAuthentication
函数中连接它startup.cs
:
app.UseCookieAuthentication(options =>
{
//other options here
options.Events = new CookieAuthenticationEvents
{
OnValidatePrincipal = UpdateValidator.ValidateAsync
};
});
UpdateValidator
函数看起来像:
public static class UpdateValidator
{
public static async Task ValidateAsync(CookieValidatePrincipalContext context)
{
//check for changes to profile here
//build new claims pricipal.
var newprincipal = new System.Security.Claims.ClaimsPrincipal();
// set and renew
context.ReplacePrincipal(newprincipal);
context.ShouldRenew = true;
}
}
SecurityStampValidator
您可以在 github 上找到该类中有一个很好的示例: https ://github.com/aspnet/Identity/blob/dev/src/Identity/SecurityStampValidator.cs