C# 比较注册表单中存在的用户名,但登录表单也受验证影响
当前的问题是登录页面还比较了exist用户名验证,在登录时提示用户“username exists”。目标是如何从登录页面删除验证,以便只有注册表单具有比较存在用户名验证。对我能做什么有什么建议吗?对于登录操作,您应该有不同的模型,它只有两个属性用户名和密码,不应该使用元数据属性。对于登录操作,您应该有不同的模型,它只有两个属性用户名和密码,不应该使用元数据属性。C# 比较注册表单中存在的用户名,但登录表单也受验证影响,c#,asp.net-mvc,C#,Asp.net Mvc,当前的问题是登录页面还比较了exist用户名验证,在登录时提示用户“username exists”。目标是如何从登录页面删除验证,以便只有注册表单具有比较存在用户名验证。对我能做什么有什么建议吗?对于登录操作,您应该有不同的模型,它只有两个属性用户名和密码,不应该使用元数据属性。对于登录操作,您应该有不同的模型,它只有两个属性用户名和密码,不应该使用元数据属性。 // This is RegisterController public class RegisterController :
// This is RegisterController
public class RegisterController : Controller
{
AccountEntities db = new AccountEntities();
// GET: Register
public ActionResult Register(int id = 0)
{
AccountTable accountModel = new AccountTable();
return View(accountModel);
}
[HttpPost]
public ActionResult Register(AccountTable userModel)
{
using (AccountEntities account = new AccountEntities())
{
account.AccountTables.Add(userModel);
account.SaveChanges();
}
ModelState.Clear();
ViewBag.SuccessMessage = "Registration Successful.";
return View("Register", new AccountTable());
}
public JsonResult IsUserExists (string Username)
{
return Json(!db.AccountTables.Any(x => x.Username == Username), JsonRequestBehavior.AllowGet);
}
}
// this is my modal
[MetadataType(typeof(UserMetaData))]
public partial class AccountTable
{
public int UserID { get; set; }
[DisplayName("User Name")]
[Required(ErrorMessage = "This field is requried.")]
[StringLength(10, MinimumLength = 4, ErrorMessage = "length between 4 than 10 characters.")]
public string Username { get; set; }
[DataType(DataType.Password)]
[StringLength(18, MinimumLength = 4, ErrorMessage = "length between 4 than 18 characters.")]
public string Password { get; set; }
[DataType(DataType.Password)]
[DisplayName("Confirm Password")]
[System.ComponentModel.DataAnnotations.Compare("Password")]
public string ConfirmPassword { get; set; }
public string Email { get; set; }
public string Contact { get; set; }
public bool isAdmin { get; set; }
public string LoginErrorMessage { get; set; }
}
public class UserMetaData
{
[Remote("IsUserExists", "Register", ErrorMessage = "User name already Exists!")]
public string Username { get; set; }
}
//this is my login controller
public class LoginController : Controller
{
// GET: Login
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Authourize(theater.Models.AccountTable user)
{
using (AccountEntities db = new AccountEntities())
{
var userDetails = db.AccountTables.Where(x => x.Username == user.Username && x.Password == user.Password).FirstOrDefault();
if (userDetails == null)
{
user.LoginErrorMessage = "Wrong username or password.";
return View("Login", user);
}
else
{
Session["userID"] = userDetails.UserID;
Session["isAdmin"] = userDetails.isAdmin;
Session["Username"] = userDetails.Username;
if (userDetails.isAdmin == true)
{
return RedirectToAction("ControlCenter", "Admin");
}
else
{
return RedirectToAction("UserHomePage", "Users");
}
}
}
}
public ActionResult LogOut()
{
Session.Abandon();
return RedirectToAction("Login", "Login");
}
}