[英]How to programmatically login to ASP.NET Identity 2?
How do you programmatically login to asp.net identity 2? 你如何以编程方式登录到asp.net身份2? This is straight forward against Web API, but MVC only project requires __RequestVerificationToken. 这是针对Web API的直接,但仅MVC项目需要__RequestVerificationToken。 This is a login method: 这是一种登录方法:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.Email, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
you must create Login View and in that ,submit your data to this method via ajax call. 您必须创建Login View,然后通过ajax调用将数据提交给此方法。 In this case I prefer using jquery.ajax method instead of ajax form . 在这种情况下,我更喜欢使用jquery.ajax方法而不是ajax形式。 Also create LoginViewModel that contains your user info like username , password so you use strongly typed view . 还要创建包含用户信息(如用户名,密码)的LoginViewModel,以便使用强类型视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.