用户登录Web应用程序时。网站必须验证用户对数据库的身份。一旦用户通过身份验证,系统就可以授权此人,以便他们可以向其他页面发出请求而无需再次登录。我在我的asp.net core 2.0 Web应用程序中实现了安全性,因为我没有使用Entity Framework。我使用MariaDB作为数据库后端。向服务器发出请求时
public ViewResult testMethod(LoginModel model){
User user = dataManager.GetUser(model.Email);
if (user == null)
{
// return user to view
return View(model);
}
if (user.Activated != true)
{
// return user to view
return View(model);
}
// Use userManager
我想使用UserManager,但由于我使用MariaDB作为后端,这还有可能吗?
答案 0 :(得分:0)
如果您正在使用,实体框架
假设您有表用户密码 尝试使用声明主体
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
if (ModelState.IsValid)
{
try
{
var user = _authService.Login(model.UserName, model.Password);
if (user != null)
{
var userClaims = new List<Claim>
{
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.NameIdentifier, user.UserId),
new Claim(ClaimTypes.Email, user.Mail),
new Claim(ClaimTypes.Role, user.PositionId)
};
var identity = new ClaimsIdentity(userClaims, "Ix");
//userClaims.Add(new Claim(ClaimTypes.Role, "0"));
var principal = new ClaimsPrincipal(identity);
await HttpContext.Authentication.SignInAsync("app", principal, new AuthenticationProperties { IsPersistent = false });
return RedirectToLocal(returnUrl);
}
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, ex.Message);
}
}
return View(model);
}