在身份验证过程中,应用程序登录管理器类为空
本文关键字:管理器 登录 应用程序 身份验证 过程中 | 更新日期: 2023-09-27 18:32:17
以下代码正在 ASP.NET MVC 5项目中使用。每次我运行以下代码时,应用程序登录管理器类总是为空,从而导致空引用异常。作为一个新手,我不明白在用户管理器和登录管理器的实例中传递帐户控制器类的构造函数中的代码调用。也许这就是我需要关注的地方,但事实是我找不到代码的那部分。有人愿意寻求帮助吗?
确切地说,异常是从 HTTPost 登录方法引发的。登录管理器始终为空。
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password,
model.RememberMe, shouldLockout: false);
下面是 c 尖锐代码
[Authorize]
public class AccountController : Controller
{
private ApplicationUserManager _userManager;
private ApplicationSignInManager _signInManager;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
//
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set { _signInManager = value; }
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
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.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
}
来自 IdentityConfig 的代码。
// Configure the application sign-in manager which is used in this application.
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);
}
}
检查Identity.Config.cs
中的以下内容:
public static void RegisterAuth(IAppBuilder app)
{
// other code
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// other code
}
在您的项目中的某个地方(可能是Startup.cs
)
[assembly: OwinStartup(typeof(Startup), "Configuration")]
namespace YourNamespace
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
AuthConfig.RegisterAuth(app);
}
}
}
如果该变量始终为nulla,则可能存在问题
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set { _signInManager = value; }
}
第一个 IME _signInManager
为 null,因此如果HttpContext.GetOwinContext().Get<ApplicationSignInManager>()
返回 null,则您的属性将为 null,并且您将收到该错误。
检查HttpContext.GetOwinContext().Get<ApplicationSignInManager>()
返回的内容,并确保其值与null