如何在自定义SignInManager中向ApplicationUser添加声明

时间:2016-02-01 00:27:28

标签: asp.net asp.net-mvc asp.net-identity owin

转换我的MVC应用程序以利用AspNet Identity。我创建了一个扩展SignInManager

的ApoplicationSignInManager类

我重写了CreateIdentityAsyc,我希望在此处添加自定义声明,而不会持久保存到db,以便这些值可供登录用户使用。

public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
    private readonly IUserService userService;

    public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager, IUserService userService)
        : base(userManager, authenticationManager)
    {
        this.userService = userService;
    }

    public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
    {
        var myUser = userService.GetUser(user.Id);

        var userIdentity = user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);

        userIdentity.Result.AddClaim(new Claim("MyCustomID", myUser.CutomID.ToString()));

        return userIdentity;
    }
}

我的问题是应用程序在执行AddClaims行时挂起。我猜这与异步任务的死锁有关,但我无法弄清楚如何解决这个问题。

请给我一些指导。

1 个答案:

答案 0 :(得分:0)

尝试在 await 方法之前使用 GenerateUserIdentityAsync

public async override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{
    ...

    var userIdentity = await user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
    ...
}
相关问题