如何以编程方式登录到 asp.net identity 2?这直接针对 Web API,但 MVC 只有项目需要 __RequestVerificationToken。这是一个登录方法:
[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);
}
最佳答案
您必须创建登录 View ,并在其中通过 ajax 调用将您的数据提交给此方法。在这种情况下,我更喜欢使用 jquery.ajax 方法而不是 ajax 表单。 同时创建包含用户名、密码等用户信息的 LoginViewModel 所以你使用强类型 View 。
关于c# - 如何以编程方式登录到 ASP.NET Identity 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23192945/