Reputation: 1873
I'm using Microsoft.Owin.Security.Cookies.dll, v3.0.0.0 and I'm trying to create an object type of CookieAuthenticationProvider using this code:
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.
}
}
After this is executed I get an error saying "An asynchronous module or handler completed while an asynchronous operation was still pending."
The method GetClaims() looks like this:
public virtual async Task<IEnumerable<Claim>> GetClaims(String test)
{
.....var claims=await GetInfo(test);
return claims;
}
If I remove the async from the OnResponseSignIn and make GetClaims() synchronous everything works.
What am I doing wrong? If I want to make OnResponseSignIn async and the GetClaims() awaitable, what is the correct way to do it?
Upvotes: 0
Views: 385
Reputation: 17485
I don't know how you retrieve claims but as per my experience with async I would do something like this.
private Task<List<Claim>> GetClaims(string name)
{
// Get your claim here.
}
In configureAuth I would do
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);
}
}
});
.....
}
Upvotes: 1