c# - SignInManager.PasswordSignInAsync keep return me null object refrence
问题描述
in my controller i use model to login these work fine and the model is give me the correct data when i log in to the account gives me null object reference
these is my model
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; }
}
and my action here
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);
}
}
and here is the configration
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
and these my startup class that is which i configure the identity
[assembly: OwinStartup(typeof(Baity.Startup))]
namespace Baity
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}
解决方案
Since the following line works
var user = UserManager.FindByName(model.UserName);
Then it means that the SignInManager
is not initialized in your controller.
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
If you share your initialization code for the SignInManager
we can help you more.
To get the manager in Identity.Config.cs
public static void RegisterAuth(IAppBuilder app)
{
// Initialize the creation
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
}
And in startup.cs
[assembly: OwinStartup(typeof(Startup), "Configuration")]
namespace MyNamespace
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
AuthConfig.RegisterAuth(app);
}
}
}
推荐阅读
- google-chrome - 在 Chrome 中中继/转发 Websocket 流量
- docker - 如何从 minikube 访问 rabbitmq 的管理 UI?
- android - 在 Android 中模拟画中画模式
- javascript - React:组件是否在每个 setState() 上完全重新渲染?
- django - 附加文件以在虚拟化中运行 Django 服务器时,“docker-compose up”挂起
- angular - 错误类型错误:无法读取未定义的属性“toLowerCase”
- java - 为记录定义默认构造函数
- intellij-idea - 更新 Intellij 后,每当我将鼠标悬停在代码上时,都会弹出这个烦人的“打开浏览器”。我该如何摆脱它?
- apache-spark - 在 EMR 的核心节点上为 Spark Application Master 预留空间
- android - 通过终端在设备中启动已安装的 android 应用程序