gpt4 book ai didi

c# - ApplicationSignInManager 类在身份验证过程中为 null

转载 作者:太空狗 更新时间:2023-10-29 17:34:07 25 4
gpt4 key购买 nike

以下代码用于 ASP.NET MVC 5 项目。每次我运行以下代码时,ApplicationSignInManager 类总是变为空,从而导致空引用异常。作为一个新手,我不明白在传递 usermanager 和登录管理器实例的 AccountController 类的构造函数中调用了什么代码。也许那是我需要关注的地方,但事实是我找不到那部分代码。有人愿意帮忙吗?

准确地说,异常是从 HTTPost 登录方法中抛出的。 signInManager 始终为空。

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

下面是c sharp代码

[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);
}
}
}

关于c# - ApplicationSignInManager 类在身份验证过程中为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27186310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com