以下代码正在ASP.NET MVC 5项目中使用。每次运行以下代码时,ApplicationSignInManager类始终为null,从而导致空引用异常。作为一个相当新手我不明白在AccountController类的构造函数中调用的代码是在usermanager的实例中传递并登录管理器。也许这就是我需要关注的地方,但事实是我无法找到代码的那一部分。任何人都可以寻求帮助吗?
确切地说,从HTTPost登录方法抛出了异常。 signInManager始终为空。
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password,
model.RememberMe, shouldLockout: false);
以下是急剧代码
[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);
}
}
答案 0 :(得分:20)
检查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);
}
}
}
答案 1 :(得分:1)
如果该变量总是为nulla,则
可能存在问题public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set { _signInManager = value; }
}
第一个ime _signInManager
为空,因此如果HttpContext.GetOwinContext().Get<ApplicationSignInManager>()
返回null,则您的属性将为null,并且您将收到该错误。
检查HttpContext.GetOwinContext().Get<ApplicationSignInManager>()
返回的内容,并确保其值不同于null