C# 如何在MVC6中实现登录用户凭证的OWIN认证?
在MVC6中,我能够用Microsoft.AspNet.Identityexcept EntityFramework实现核心ADO.net。但身份验证仍然挂起,因为我不知道当用户登录状态时如何在MVC6中维护身份验证 在MVC6中,有自己的演示项目,它在实体框架中维护登录用户凭证的身份验证 但我希望在MVC6中实现具有身份验证的核心ADO.Net实现 因此,如果这些人知道如何在MVC6中对登录用户进行身份验证 我的登录操作方法:C# 如何在MVC6中实现登录用户凭证的OWIN认证?,c#,asp.net-mvc,asp.net-identity,C#,Asp.net Mvc,Asp.net Identity,在MVC6中,我能够用Microsoft.AspNet.Identityexcept EntityFramework实现核心ADO.net。但身份验证仍然挂起,因为我不知道当用户登录状态时如何在MVC6中维护身份验证 在MVC6中,有自己的演示项目,它在实体框架中维护登录用户凭证的身份验证 但我希望在MVC6中实现具有身份验证的核心ADO.Net实现 因此,如果这些人知道如何在MVC6中对登录用户进行身份验证 我的登录操作方法: public async Task<ActionResult
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await _customerUserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
// await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError(string.Empty, "Invalid username or password.");
}
}
return View(model);
}
您需要创建一个ClaimsIdentity并将其放入Authentication.SignIn方法中。然后Identity生成相关cookie并让您的用户登录:
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await _customerUserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
var options=new IdentityOptions();
var ident = new ClaimsIdentity(IdentityOptions.ApplicationCookieAuthenticationType);
ident.AddClaims(new[]
{
new Claim(options.ClaimsIdentity.UserIdClaimType,user.Id),
new Claim(options.ClaimsIdentity.UserNameClaimType, user.userName),
// populate assigned user roles form the DB and add each one as a claim
new Claim(ClaimTypes.Role,"RoleName1"),
new Claim(ClaimTypes.Role,"RoleName2"),
});
Context.Authentication.SignIn(IdentityOptions.ApplicationCookieAuthenticationScheme, new ClaimsPrincipal(ident));
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError(string.Empty, "Invalid username or password.");
}
}
return View(model);
}
你的问题不清楚。您是否已成功使用Identity实现ADO.net?当用户登录状态时,说在MVC 6中维护身份验证是什么意思?我在MVC6中成功实现了具有标识的ADO.net。我的问题是,当用户登录状态时,如何在应用程序中维护他们的凭据。我谈论的是无状态概念。注释不用于扩展讨论;这段对话已经结束。我给你发了一封电子邮件。我希望有帮助。