登录请求中未设置ASP.NET标识

登录请求中未设置ASP.NET标识,asp.net,asp.net-mvc,asp.net-identity-2,Asp.net,Asp.net Mvc,Asp.net Identity 2,我正在使用ASP.NET身份和ClaimsIdentity对我的用户进行身份验证。对用户进行身份验证时,属性user.Identity包含一个ClaimsIdentity实例 但是,在登录请求期间并非如此: public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) {

我正在使用ASP.NET身份和
ClaimsIdentity
对我的用户进行身份验证。对用户进行身份验证时,属性
user.Identity
包含一个
ClaimsIdentity
实例

但是,在登录请求期间并非如此:

    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, false, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                var identity = User.Identity;

------------>   // identity is of type WindowsIdentity  <------------

                return RedirectToLocal(returnUrl);
公共异步任务登录(LoginViewModel模型,字符串返回URL)
{
如果(!ModelState.IsValid)
{
返回视图(模型);
}
var result=await-SignInManager.PasswordSignInAsync(model.Email,model.Password,false,shouldllockout:false);
开关(结果)
{
案例标志状态成功:
var identity=User.identity;

------------>//identity的类型为WindowsIdentity我同意PasswordSignInAsync没有将用户实例作为out参数返回太糟糕了。如果查看源代码,可以看到它使用了
this.UserManager.FindByNameAsync(userName)
按用户名查找用户,然后使用
this.UserManager.CheckPasswordAsync(user,password)
验证密码,但用户存储在局部变量中

我认为这应该作为对SignInManager类的改进而提出

一种可能的(且效率低下的)解决方法是再次查询用户,然后以与SignInManager.SignInAsync相同的方式创建您自己的声明标识:

        ApplicationUser user = await UserManager.FindByNameAsync(model.Email);
        ClaimsIdentity claimsIdentity = await SignInManager.CreateUserIdentityAsync(user);

身份通过
AuthenticationResponseGrant
属性公开:

ClaimsIdentity identity = SignInManager.AuthenticationManager.AuthenticationResponseGrant.Identity;

使用
RedirectToAction
并在该操作中获取标识?与其返回URL,不如返回您要重定向到的视图的名称。这是我目前正在使用的解决方法。但是,在登录操作中是否有可能获取
ClaimSidenty
?这对我来说非常有效,我相信它没有ineff奥古斯托的回答(这是我的第一个倾向)中包含了冷淡。我建议让这成为公认的答案。