asp.net SignInManager.PasswordSignInAsync保持返回空对象引用

x4shl7ld  于 11个月前  发布在  .NET
关注(0)|答案(3)|浏览(136)

在我的控制器我使用模型登录这些工作罚款和模型是给予我正确的数据当我登录到帐户给我空对象引用
这是我的模型

public class LoginViewModel
    {
        [Required]
        //[Display(Name = "Email")]

        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }

字符串
我在这里的行动

public ApplicationSignInManager SignInManager { 
   get { return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>(); 
   } 
   private set { _signInManager = value; } 
}

public async Task<ActionResult> LoginUser(LoginViewModel model, string returnUrl)
       {
           if (!ModelState.IsValid)
           {
               return View(model);
           }
           var user = UserManager.FindByName(model.UserName);

           var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
           switch (result)
           {
               case SignInStatus.Success:
                   return RedirectToLocal(returnUrl);
               case SignInStatus.LockedOut:
                   return View("Lockout");
               case SignInStatus.Failure:
               default:
                   ModelState.AddModelError("", "Invalid login attempt.");
                   return View(model);
           }
       }


这是一个

public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
    {
        public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
            : base(userManager, authenticationManager)
        {
        }

        public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
        {
            return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
        }
        public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
        {
            var d = new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
            return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
        }
    }


and here the error in the browser
这些是我的启动类,我配置了标识,

[assembly: OwinStartup(typeof(Baity.Startup))]

namespace Baity
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {

           ConfigureAuth(app);

        }

    }
}

nszi6y05

nszi6y051#

由于下面的行可以工作,

var user = UserManager.FindByName(model.UserName);

字符串
这意味着SignInManager没有在控制器中初始化。

var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);


如果您分享SignInManager的初始化代码,我们可以为您提供更多帮助。
Identity.Config.cs中获取管理器

public static void RegisterAuth(IAppBuilder app)
{
   // Initialize the creation
   app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

}


在startup.cs中,

[assembly: OwinStartup(typeof(Startup), "Configuration")]    
namespace MyNamespace
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            AuthConfig.RegisterAuth(app);
        }
    }
}

l5tcr1uw

l5tcr1uw2#

我发现_signInManager = signInManager;在Controller类的构造函数中丢失了,所以要仔细检查。

private readonly SignInManager<ApplicationUser> _signInManager;

 public (AccountController(SignInManager<ApplicationUser> signInManager)
 {
     //this was the issue as it was missing so add it should work 
       _signInManager = signInManager;
 }

字符串

kb5ga3dv

kb5ga3dv3#

我发现我的问题的答案是,在身份验证,这就是为什么它不为我工作

public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
    public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
        : base(userManager, authenticationManager)
    {
    }

    public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
    {
        return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
    }

    public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
    {
        return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
    }
}

字符串

相关问题