// In this method we will create default User roles and Admin user for login private void createRolesandUsers() { ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new 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.Email = "Gaving30gmail.com"; string userPWD = "testPassword1."; var chkUser = UserManager.Create(user, userPWD); //Add default User to Role Admin if (chkUser.Succeeded) { var result1 = UserManager.AddToRole(user.Id, "Admin"); } } }
internal void CreateUserAndRole() { using (var context = new WebDeveloperDbContext()) { var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); var userManager = new UserManager<WebDeveloperUser>(new UserStore<WebDeveloperUser>(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 IdentityRole(); role.Name = "Admin"; roleManager.Create(role); //Here we create a Admin super user who will maintain the website var user = new WebDeveloperUser { UserName = "******", Email = "*****@*****.**" }; string userPassword = "******"; var userCreation = userManager.Create(user, userPassword); //Add default User to Role Admin if (userCreation.Succeeded) userManager.AddToRole(user.Id, "Admin"); } // creating Creating Manager role if (!roleManager.RoleExists("Manager")) { var role = new IdentityRole { Name = "Manager" }; roleManager.Create(role); } // creating Creating Employee role if (!roleManager.RoleExists("Employee")) { var role = new IdentityRole { Name = "Employee" }; roleManager.Create(role); } } }
// In this method we will create default User roles and Admin user for login private void createRolesandUsers() { ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new 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); //Creacion del superadmin var user = new ApplicationUser(); user.UserName = "******"; user.Email = "*****@*****.**"; string userPWD = "123456"; var chkUser = UserManager.Create(user, userPWD); //Rol por defecto que es Admin if (chkUser.Succeeded) { var result1 = UserManager.AddToRole(user.Id, "Admin"); } } // Creacion de nuevos roles if (!roleManager.RoleExists("Cliente")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Cliente"; roleManager.Create(role); } }
protected void CreateUser_Click(object sender, EventArgs e) { var manager = new UserManager(); var user = new ApplicationUser() { UserName = UserName.Text }; IdentityResult result = manager.Create(user, Password.Text); if (result.Succeeded) { IdentityHelper.SignIn(manager, user, isPersistent: false); using (Logic.ShoppingCartActions usersShoppingCart = new Logic.ShoppingCartActions()) { String cartId = usersShoppingCart.GetCartId(); usersShoppingCart.MigrateCart(cartId, user.Id); } IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
private void createRolesandUsers() { ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); if (!roleManager.RoleExists("Admin")) { var role = new IdentityRole(); role.Name = "Admin"; roleManager.Create(role); var user = new ApplicationUser(); user.UserName = "******"; user.Email = "*****@*****.**"; string userPWD = "Test#1"; var chkUser = UserManager.Create(user, userPWD); if (chkUser.Succeeded) { var result1 = UserManager.AddToRole(user.Id, "Admin"); } } if (!roleManager.RoleExists("Educator")) { var role = new IdentityRole(); role.Name = "Educator"; roleManager.Create(role); } if (!roleManager.RoleExists("Student")) { var role = new IdentityRole(); role.Name = "Student"; roleManager.Create(role); } }
private static void CreateAdminUser(UserManager<ApplicationUser> userManager) { var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ContosoWebContext(StaticConfig.DbContext.WebConnectionStringName))); if (!roleManager.RoleExists(AdminConstants.Role)) { roleManager.Create(new IdentityRole(AdminConstants.Role)); } var username = ConfigurationHelpers.GetString("Authentication.Administrator.UserName"); var password = ConfigurationHelpers.GetString("Authentication.Administrator.Password"); var user = userManager.FindByName(username); if (user == null) { user = new ApplicationUser { UserName = username, Email = username }; var result = userManager.Create(user, password); if (!result.Succeeded) throw new Exception(string.Format("Failed to create admin user: {0}", string.Join(",", result.Errors))); user = userManager.FindByName(username); userManager.AddToRole(user.Id, AdminConstants.Role); userManager.AddClaim(user.Id, new Claim(AdminConstants.ManageStore.Name, AdminConstants.ManageStore.Allowed)); } }
public void Configuration(IAppBuilder app) { ConfigureAuth(app); #region Add Admin and Role for Admin using (var context = new ApplicationDbContext()) { var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); var aspUser = UserManager.FindByName(Constant.USER_USERNAME); if (aspUser != null) { UserManager.RemovePassword(aspUser.Id); UserManager.AddPassword(aspUser.Id, Constant.USER_PASS); } else { aspUser = new ApplicationUser() { UserName = Constant.USER_USERNAME }; var result = UserManager.Create(aspUser, Constant.USER_PASS); if (result.Succeeded) { var user = new User { UserName = Constant.USER_USERNAME, Email = Constant.USER_EMAIL, Phone = Constant.USER_PHONE, Address = Constant.USER_ADDRESS, AspnetId = aspUser.Id }; using (var db = new TShirtEntities()) { db.User.Add(user); db.SaveChanges(); } } } // Create Role IdentityResult roleResult = null; if (!RoleManager.RoleExists(Constant.ROLES_ADMIN)) { roleResult = RoleManager.Create(new IdentityRole(Constant.ROLES_ADMIN)); } // Add role to admin if (roleResult != null && roleResult.Succeeded) UserManager.AddToRole(aspUser.Id, Constant.ROLES_ADMIN); } #endregion #region Add Constants Config var lstConfig = new List<Config>(); using (var db = new TShirtEntities()) { var fb = db.Config.FirstOrDefaultAsync(m => m.Code == Constant.CODE_MESS_FACEBOOK); var price = db.Config.FirstOrDefaultAsync(m => m.Code == Constant.CODE_PRICE_DESIGN); if (fb == null) { lstConfig.Add(new Config() { Code = Constant.CODE_MESS_FACEBOOK, Value = "Website thiết kế áo chuyên nghiệp", Description = "Nội dung khi chia sẻ trên facebook" }); } if (price == null) { lstConfig.Add(new Config() { Code = Constant.CODE_PRICE_DESIGN, Value = "15000", Description = "Giá một icon design" }); } if (lstConfig.Count > 0) { db.Config.AddRange(lstConfig); db.SaveChanges(); } } #endregion }
private static void SetupUsers(ApplicationDbContext db) { using (var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()))) using (var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))) { // Creating roles foreach (var role in Enum.GetValues(typeof(Constants.UserRole))) { if (rm.RoleExists(role.ToString())) continue; var result = rm.Create(new IdentityRole(role.ToString())); if (!result.Succeeded) throw new ApplicationException("Creating role " + role + " failed with error(s):\n" + GetAllErrors(result)); } // Creating users foreach (var newUser in UsersToSetup) { var existingUser = um.FindByEmail(newUser.Email); if (existingUser == null) { var result = um.Create(new ApplicationUser { Email = newUser.Email, EmailConfirmed = true, UserName = newUser.Email, LockoutEnabled = newUser.LockoutEnabled }, newUser.Password); if (!result.Succeeded) throw new ApplicationException("Creating user " + newUser.Email + " failed with error(s):\n" + GetAllErrors(result)); } existingUser = um.FindByEmail(newUser.Email); if (!um.IsInRole(existingUser.Id, Constants.UserRole.Admin.ToString())) { var result = um.AddToRole(existingUser.Id, Constants.UserRole.Admin.ToString()); if (!result.Succeeded) throw new ApplicationException("Adding role " + Constants.UserRole.Admin + " for " + newUser.Email + " failed with error(s):\n" + GetAllErrors(result)); } } db.SaveChanges(); } }
private static void AddUser(ApplicationDbContext context, string userName, string role) { var userStore = new UserStore<ApplicationUser>(context); var userManager = new UserManager<ApplicationUser>(userStore); if (!context.Users.Any(u => u.UserName == userName)) { var user = new ApplicationUser { UserName = userName, Email = userName }; IdentityResult result = userManager.Create(user, "Passw0rd!"); userManager.AddToRole(user.Id, role); context.SaveChanges(); } }
//Create default User roles and Admin user for login private void CreateRolesandUsers() { ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); // Creating first Admin Role and creating a default Admin User if (!roleManager.RoleExists("Admin")) { // Create Admin roll var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Admin"; roleManager.Create(role); // Create an Admin super user who will maintain the website var user = new ApplicationUser(); user.UserName = "******"; user.FirstName = "Admin"; user.LastName = "RDCC"; user.EmailAddress = "*****@*****.**"; string userPWD = "Rdcc1234!"; var chkUser = userManager.Create(user, userPWD); var adminRole = roleManager.FindByName("Admin"); //Add default User to Role Admin if (chkUser.Succeeded) { var result1 = userManager.AddToRole(user.Id, "Admin"); } } // creating Creating Member role if (!roleManager.RoleExists("Member")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Member"; roleManager.Create(role); } // creating Creating Chairman role if (!roleManager.RoleExists("Chairman")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Chairman"; roleManager.Create(role); } // creating Creating Secretary role if (!roleManager.RoleExists("Secretary")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Secretary"; roleManager.Create(role); } // creating Creating Programme Secretary role if (!roleManager.RoleExists("Programme Secretary")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Programme Secretary"; roleManager.Create(role); } // creating Creating Treasurer role if (!roleManager.RoleExists("Treasurer")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Treasurer"; roleManager.Create(role); } // creating Creating Committee Member role if (!roleManager.RoleExists("Committee Member")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Committee Member"; roleManager.Create(role); } // creating Creating Webmaster role if (!roleManager.RoleExists("Webmaster")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Webmaster"; roleManager.Create(role); } if (!roleManager.RoleExists("Publicity Officer")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Publicity Officer"; roleManager.Create(role); } }
public void ConfigureAuth(IAppBuilder app) { app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions()); app.UseWsFederationAuthentication( new WsFederationAuthenticationOptions { Wtrealm = realm, MetadataAddress = adfsMetadata, //custom code below to respond to notifications Notifications = new WsFederationAuthenticationNotifications { AuthenticationFailed = context => { return Task.FromResult(0); }, MessageReceived = context => { return Task.FromResult(0); }, RedirectToIdentityProvider = context => { return Task.FromResult(0); }, SecurityTokenReceived = context => { return Task.FromResult(0); }, SecurityTokenValidated = context => { // Is this the point I can connect this authenticated user to the Identity database (roles) ? /* context.AuthenticationTicket.Identity.AddClaim( new Claim(ClaimTypes.Role, "ModelEditorRole") ); context.AuthenticationTicket.Identity.AddClaim( new Claim(ClaimTypes.Role, "SomeOtherRole") ); */ string accountName = ""; string accountEmail = ""; foreach (var claim in context.AuthenticationTicket.Identity.Claims) { // instead of using "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn" // use enum if (claim.Type == ClaimTypes.Upn) { accountName = claim.Value; //do something? Connect to ASP.NET Identity database? // Can we "log in" an account in asp.net identity matching the user identity // e.g. [email protected]? // or do we add the account into the identity database, and only use to query roles // from the identity database ? // mainly, we want the current principal to pass an // [Authorize roles("roleX")] test // how about context.AuthenticationTicket.Identity.AddClaim() ? } else if (claim.Type == ClaimTypes.Email) { accountEmail = claim.Value; } else if (claim.Type == ClaimTypes.Role) { // these would be roles coming from Active Directory, not the Identity database string roleName = claim.Value; } } var db = new ApplicationDbContext(); //Add the user if necessary var user = (from u in db.Users where u.UserName.ToLower() == accountName.ToLower() select u).FirstOrDefault(); //get the users roles and add to the claims if (user == null) { var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db)); var adminUser = new ApplicationUser() { UserName = accountName, Email = accountEmail }; var ir = um.Create(adminUser, "placeholder#123X"); // not sure how I feel about this... does it need a password? } var userRoles = (from u in db.Users from ur in u.Roles join r in db.Roles on ur.RoleId equals r.Id where u.UserName.ToLower() == accountName.ToLower() select new { RoleName = r.Name }).ToList(); foreach (var role in userRoles) { context.AuthenticationTicket.Identity.AddClaim( new Claim(ClaimTypes.Role, role.RoleName)); } return Task.FromResult(0); } } }); }