我的用户模型如下所示:
public class User
{
[Key]
[Required]
[StringLength(30, MinimumLength = 3)]
[Display(Name = "Username")]
public String Name { get; set; }
[EmailAddress]
[Required]
[MaxLength(254)]
[Display(Name="Email address")]
public String Email { get; set; }
[Required]
[DataType(DataType.Password)]
[StringLength(20, MinimumLength = 6)]
public String Password { get; set; }
休息无关紧要。
我有以下问题:当我尝试验证登录时,我需要输入一个电子邮件地址,因为如果我不这样做,它会返回[Required]
并且登录逻辑将被跳过......我无法删除因为我需要在注册中使用它,但我还想让用户只用用户名登录。有没有办法只在注册时才需要电子邮件?如果不是您建议我做的,请从电子邮件中删除 Require?ModelState.IsValid
false
[Required]
登录:
if (ModelState.IsValid)
{
if (IsValid(user.Name, user.Password))
{
FormsAuthentication.SetAuthCookie(user.Name, false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Login Data is incorrect");
}
}
并注册:
if (ModelState.IsValid)
{
using (var db = new CardContext())
{
var crypto = new SimpleCrypto.PBKDF2();
var encrPass = crypto.Compute(user.Password);
var sysUser = db.Users.Create();
sysUser.Avatar = null;
sysUser.Banned = false;
sysUser.Email = user.Email;
sysUser.Information = "";
sysUser.Name = user.Name;
sysUser.Password = encrPass;
sysUser.Rank = null;
sysUser.Salt = crypto.Salt;
db.Users.Add(sysUser);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "Registration data is incorrect.");
}