下面code正在使用ASP.NET MVC中5项目。每次我运行C的ApplicationSignInManager级以下$ C $总是为空导致空引用异常。作为还算是一个新手,我不明白美元的的AccountController类传入的UserManager的实例构造函数C $的C调用和登录管理器。也许这正是我需要关注,但事实是我无法找到的code部分。任何人都为帮助?
要为precise异常是从HTTPost登录方法抛出。该signInManager总是空。
VAR的结果=等待SignInManager.PasswordSignInAsync(model.UserName,model.Password,
model.RememberMe,shouldLockout:假);
下面是升C code
[授权]
公共类的AccountController:控制器
{
私人ApplicationUserManager _userManager;
私人ApplicationSignInManager _signInManager;
公众的AccountController()
{
}
公众的AccountController(ApplicationUserManager的UserManager,ApplicationSignInManager signInManager)
{
的UserManager =的UserManager;
SignInManager = signInManager;
}
//
// GET:/帐号/登录
[使用AllowAnonymous]
公众的ActionResult登录(字符串RETURNURL)
{
ViewBag.ReturnUrl = RETURNURL;
返回查看();
}
公共ApplicationSignInManager SignInManager
{
得到
{
返回_signInManager? 。HttpContext.GetOwinContext()获取< ApplicationSignInManager>();
}
私人集合{_signInManager =价值; }
}
//
//邮编:/帐号/登录
[HttpPost]
[使用AllowAnonymous]
[ValidateAntiForgeryToken]
公共异步任务<的ActionResult>登录(LoginViewModel型号,串RETURNURL)
{
如果(!ModelState.IsValid)
{
返回查看(模型);
}
//这并不算登录失败对帐户锁定
//启用密码失败触发帐户锁定,更改为shouldLockout:真
VAR的结果=等待SignInManager.PasswordSignInAsync(model.UserName,model.Password,model.RememberMe,shouldLockout:假);
开关(结果)
{
案例SignInStatus.Success:
返回RedirectToLocal(RETURNURL);
案例SignInStatus.LockedOut:
返回视图(锁定);
案例SignInStatus.RequiresVerification:
返回RedirectToAction(送code,新{RETURNURL = RETURNURL,了rememberMe = model.RememberMe});
案例SignInStatus.Failure:
默认:
ModelState.AddModelError(,无效的登录尝试。);
返回查看(模型);
}
}
}
code从IdentityConfig。
//配置应用程序登录管理器,它是用在这个应用程序。
公共类ApplicationSignInManager:SignInManager< ApplicationUser,串>
{
公共ApplicationSignInManager(ApplicationUserManager的UserManager,IAuthenticationManager AuthenticationManager会)
:基地(的UserManager,AuthenticationManager会)
{
}
公众覆盖任务< ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser用户)
{
返回user.GenerateUserIdentityAsync((ApplicationUserManager)的UserManager);
}
公共静态ApplicationSignInManager创建(IdentityFactoryOptions< ApplicationSignInManager>选项,IOwinContext上下文)
{
返回新ApplicationSignInManager(context.GetUserManager< ApplicationUserManager>(),context.Authentication);
}
}
解决方案
检查里面的按照你的 Identity.Config.cs
:
公共静态无效RegisterAuth(IAppBuilder应用程序)
{
//其他code
app.CreatePerOwinContext< ApplicationSignInManager>(ApplicationSignInManager.Create);
//其他code
}
和地方在你的项目(可能是 Startup.cs
)
[总成:OwinStartup(typeof运算(启动),配置)
命名空间YourNamespace
{
公共类启动
{
公共无效配置(IAppBuilder应用程序)
{
AuthConfig.RegisterAuth(应用程序);
}
}
}
The following code is being used in ASP.NET MVC 5 project. Everytime I run the following code the ApplicationSignInManager Class always comes to be null causing null reference exception. Being fairly a newbie I don't understand what code calls in the constructor of AccountController class passing in the instance of usermanager and sign in manager. perhaps that is where I need to focus but the truth is I can't find that part of the code. Anyone up for help?
To be precise the exception is thrown from the HTTPost login method. The signInManager is always null.
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password,
model.RememberMe, shouldLockout: false);
Below is the c sharp code
[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);
}
}
}
Code from 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);
}
}
解决方案
Check for the following inside your Identity.Config.cs
:
public static void RegisterAuth(IAppBuilder app)
{
// other code
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// other code
}
and somewhere in your project (possibly Startup.cs
)
[assembly: OwinStartup(typeof(Startup), "Configuration")]
namespace YourNamespace
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
AuthConfig.RegisterAuth(app);
}
}
}
上一篇:哪些单位电池温度和电压的Android设备Intent.BATTERY_ACTION_CHANGED什么时候?什么时候、电压、温度、电池
下一篇:覆盖的Android-L CardView STATE_ pressed对于旧版本的Android系统旧版本、系统、oid、Andr