我的观点是能够使用SimpleMembership。首先,我在Global.asax.cs类中创建了角色。
Database.SetInitializer<UsersContext>(new InitSecurityDb());
UsersContext context = new UsersContext();
context.Database.Initialize(true);
if (!WebSecurity.Initialized)
WebSecurity.InitializeDatabaseConnection("DefaultConnection",
"UserProfile", "UserId", "UserName", autoCreateTables: true);
var roles = (SimpleRoleProvider)Roles.Provider;
var membership = (SimpleMembershipProvider)Membership.Provider;
if (!roles.RoleExists("Manager"))
{
roles.CreateRole("Manager");
}
if (!roles.RoleExists("Developer"))
{
roles.CreateRole("Developer");
}
然后我根据用户类型向用户添加了一个角色。如果用户类型是管理员,我想将管理员角色添加到我的用户。我发布的注册行动:
public ActionResult Register(RegisterModel model)
{
var roles = (SimpleRoleProvider)Roles.Provider;
var membership = (SimpleMembershipProvider)Membership.Provider;
if (membership.GetUser(model.UserName, false) == null)
{
membership.CreateUserAndAccount(model.UserName, model.Password);
}
if(model.status == UserType.Manager)
{
if (!roles.GetRolesForUser(model.UserName).Equals("Manager"))
{
roles.AddUsersToRoles(new[] { model.UserName }, new[] { "manager" });
}
}
if (model.status == UserType.Developer)
{
if (!roles.GetRolesForUser(model.UserName).Equals("Developer"))
{
roles.AddUsersToRoles(new[] { model.UserName }, new[] { "developer" });
}
}
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { UserName = model.UserName, Password = model.Password, firstName = model.firstName, lastName = model.lastName, Email = model.Email, status = model.status });
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
在membership中的成员.CreateUserAndAccount(model.UserName,model.Password)&#34;注册操作中的行我收到以下错误:
类型&#39; System.Data.SqlClient.SqlException&#39;的异常发生在System.Data.dll中但未在用户代码中处理。 附加信息:无法将值NULL插入列&#39; status&#39;,table&#39; MyApplication.dbo.UserProfile&#39;列不允许空值。 INSERT失败。
我的userProfile类
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
[Required(ErrorMessage = "Lütfen adınızı giriniz.")]
public string firstName { get; set; }
[Required(ErrorMessage = "Lütfen soyadınızı giriniz.")]
public string lastName { get; set; }
[DataType(DataType.Password)]
[Required(ErrorMessage = "Lütfen şifrenizi giriniz...")]
public string Password { get; set; }
[Required(ErrorMessage = "Lütfen e-posta adresinizi giriniz.")]
[DataType(DataType.EmailAddress, ErrorMessage = "Lütfen e-posta adresinizi geçerli bir formatta giriniz.")]
public string Email { get; set; }
public UserType status { get; set; }
}
最后是我的注册模型:
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string Email { get; set; }
public UserType status { get; set; }
}
提前致谢。
答案 0 :(得分:0)
您应该查看您的表并确保列状态是可以为空的字段。如果它是必填字段,如果您尝试在其中输入空值,则会收到该错误消息。我想将此添加为评论,但我没有足够的权限来添加评论。