我正在使用Microsoft.Owin.Security.Cookies.dll,v3.0.0.0,并尝试使用此代码创建CookieAuthenticationProvider的对象类型:
var provider= new CookieAuthenticationProvider
{
OnResponseSignIn = async context =>
{
var claims=await GetClaims(identity.Name); //I get the claims here.
context.Identity.AddClaims(clais); //The claims are being added.
}
}
执行此操作后,我收到错误消息"异步模块或处理程序在异步操作仍未完成时已完成。"
方法GetClaims()如下所示:
public virtual async Task<IEnumerable<Claim>> GetClaims(String test)
{
.....var claims=await GetInfo(test);
return claims;
}
如果我从OnResponseSignIn中移除异步并使GetClaims()同步一切正常。
我做错了什么?如果我想让OnResponseSignIn异步和GetClaims()等待,那么正确的方法是什么?
答案 0 :(得分:1)
我不知道你是如何检索索赔的,但根据我对异步的经验,我会做这样的事情。
private Task<List<Claim>> GetClaims(string name)
{
// Get your claim here.
}
在configureAuth中我会做
public void ConfigureAuth(IAppBuilder app)
{
Func<CookieResponseSignInContext, Task> asyncFunc = async ctx =>
{
var claims = await GetClaims(ctx.Identity.Name);
ctx.Identity.AddClaims(claims);
};
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),
OnResponseSignIn = context =>
{
asyncFunc(context);
}
}
});
.....
}