private void createRolesandUsers(string username, string userType) { ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new Microsoft.AspNet.Identity.RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var UserManager = new Microsoft.AspNet.Identity.UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); if (!roleManager.RoleExists("Employee") && userType == "1") { // first we create Admin rool var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Employee"; roleManager.Create(role); } if (!roleManager.RoleExists("Customer") && userType == "2") { // first we create Admin rool var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Customer"; roleManager.Create(role); } string s; if (userType == "1") { s = "Employee"; } else { s = "Customer"; } var result1 = UserManager.AddToRole(username, s); }
// In this method we will create default User roles and Admin user for login private void createRolesandUsers() { Models.ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new Microsoft.AspNet.Identity.RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); // In Startup iam creating first Admin Role and creating a default Admin User if (!roleManager.RoleExists("Admin")) { // first we create Admin rool var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Admin"; roleManager.Create(role); //Here we create a Admin super user who will maintain the website var user = new ApplicationUser(); user.UserName = "******"; user.Email = "*****@*****.**"; string userPWD = "P@ssw0rd"; var chkUser = UserManager.Create(user, userPWD); //Add default User to Role Admin if (chkUser.Succeeded) { var result1 = UserManager.AddToRole(user.Id, "Admin"); } } // creating Creating Manager role if (!roleManager.RoleExists("User")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "User"; roleManager.Create(role); } // creating Creating Employee role if (!roleManager.RoleExists("Employee")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Employee"; roleManager.Create(role); } }
public void createRole() { var manger = new Microsoft.AspNet.Identity.RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db)); IdentityRole role = new IdentityRole(); if (!manger.RoleExists("Admin")) { role.Name = "Admin"; manger.Create(role); } if (!manger.RoleExists("User")) { role.Name = "User"; manger.Create(role); } }
private void CreateRolesAndUsers() { ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new Microsoft.AspNet.Identity.RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var UserManager = new Microsoft.AspNet.Identity.UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); if (!roleManager.RoleExists("Admin")) { // first we create Admin rool var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Admin"; roleManager.Create(role); var user = new ApplicationUser(); user.UserName = "******"; user.Email = "*****@*****.**"; string userPWD = "HuaTheDogN0se!"; var chkUser = UserManager.Create(user, userPWD); if (chkUser.Succeeded) { var result1 = UserManager.AddToRole(user.Id, "Admin"); } } // creating Creating Manager role if (!roleManager.RoleExists("Employee")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Employee"; roleManager.Create(role); } // creating Creating Employee role if (!roleManager.RoleExists("Customer")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Customer"; roleManager.Create(role); } }
public ActionResult Register(RegisterModel model, HttpPostedFileBase file) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Home")); } else { if (ModelState.IsValid) { var path = Server.MapPath("~/Upload/Users"); //dosya yolu var filename = Path.ChangeExtension(model.Username + "", ".jpg"); if (file == null) { filename = "place.jpg"; } var fullpath = Path.Combine(path, filename); file.SaveAs(fullpath); model.UserImage = filename.ToString(); //Kayıt İşlemleri ApplicationUser user = new ApplicationUser(); user.Name = model.Name; user.Surname = model.Surname; user.Email = model.Email; user.UserName = model.Username; IdentityResult result = userManager.Create(user, model.Password); if (result.Succeeded) { //Kullanıcı oluştu ve rol atanabilir. if (roleManager.RoleExists("user")) { userManager.AddToRole(user.Id, "user"); } return(RedirectToAction("Login", "Account")); } else { ModelState.AddModelError("RegisterUserError", "Kullanıcı oluşturulamadı."); } } return(View(model)); } }
private void CreateRoles() { ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new Microsoft.AspNet.Identity.RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); if (!roleManager.RoleExists("Visitor")) { var role = new IdentityRole(); role.Name = "Visitor"; roleManager.Create(role); } if (!roleManager.RoleExists("Admin")) { var role = new IdentityRole(); role.Name = "Admin"; roleManager.Create(role); } }
public ActionResult AddRole(string RoleName) { var roleManager = new Microsoft.AspNet.Identity.RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db)); IdentityRole role = new IdentityRole(); if (!roleManager.RoleExists(RoleName)) { role = new IdentityRole(); role.Name = RoleName; roleManager.Create(role); } return(RedirectToAction("CreateRoles")); }
public ActionResult CreateRole(FormCollection form) { ViewBag.RoleUyari = ""; string RoleName = form["RoleName"]; var roleManager = new Microsoft.AspNet.Identity.RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); if (!roleManager.RoleExists(RoleName)) { var role = new IdentityRole(RoleName); roleManager.Create(role); } else { ViewBag.RoleUyari = "The role you want to add is already registered."; return(View()); } return(View()); }
private void createRolesandUsers(string username) { ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new Microsoft.AspNet.Identity.RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var UserManager = new Microsoft.AspNet.Identity.UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); // In Startup iam creating first Admin Role and creating a default Admin User if (!roleManager.RoleExists("Administrator")) { // first we create Admin rool var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Administrator"; roleManager.Create(role); } var result1 = UserManager.AddToRole(username, "Administrator"); }