My point is to able to use SimpleMembership. Firstly, I created roles in my Global.asax.cs class.
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");
}
and then I added a role to user according to user type. if user type is manager, I want to add the manager role to my user. my posted register action :
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);
}
in the "membership.CreateUserAndAccount(model.UserName, model.Password)" line in Register action I got below error :
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code. Additional information: Cannot insert the value NULL into column 'status', table 'MyApplication.dbo.UserProfile'; column does not allow nulls. INSERT fails.
My userProfile class
[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; }
}
and finally my Register model:
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; }
}
Thanks in advance.