gpt4 book ai didi

c# - 如何检查登录操作中是否存在用户?

转载 作者:太空狗 更新时间:2023-10-29 22:05:37 24 4
gpt4 key购买 nike

我开始使用新的身份管理并且有一个简单的需求。当我的用户使用错误的名称登录时,它会报告密码错误。我该如何更改它,以便它还使用 dbcontext 方法检查用户名是否存在?

    public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
// Validate the password
IdentityResult result = IdentityManager.Authentication.CheckPasswordAndSignIn(AuthenticationManager, model.UserName, model.Password, model.RememberMe);
if (result.Success)
{
return Redirect("~/home");
}
else
{
AddErrors(result);
}
}

// If we got this far, something failed, redisplay form
return View(model);
}

最佳答案

我是这样做的:

        var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

if (UserManager.FindByName("Admin") == null)
{
var user = new ApplicationUser() { UserName = "Admin" };
UserManager.Create(user, "Admin123");
UserManager.AddToRole(user.Id, "Admin");
}

关于c# - 如何检查登录操作中是否存在用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19605061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com