在文章中,他們引用了代表中的CookieValidatePrincipalContext
代表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
從文章中,'context'看起來像是一個CookieValid atePrincipalContext'。 – DavidG
如何訪問CookieValidatePrincipalContext的方法?我一直在做這方面的研究,因爲你給了我答案,但我一直無法弄清楚如何使用它。謝謝。 – Sam
https://docs.asp.net/projects/api/en/latest/autoapi/Microsoft/AspNet/Authentication/Cookies/CookieValidatePrincipalContext/ – DavidG