本文整理汇总了C#中UserManager.AddToRole方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.AddToRole方法的具体用法?C# UserManager.AddToRole怎么用?C# UserManager.AddToRole使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.AddToRole方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Initialize
public static void Initialize(MacheteContext DB)
{
IdentityResult ir;
var rm = new RoleManager<IdentityRole>
(new RoleStore<IdentityRole>(DB));
ir = rm.Create(new IdentityRole("Administrator"));
ir = rm.Create(new IdentityRole("Manager"));
ir = rm.Create(new IdentityRole("Check-in"));
ir = rm.Create(new IdentityRole("PhoneDesk"));
ir = rm.Create(new IdentityRole("Teacher"));
ir = rm.Create(new IdentityRole("User"));
ir = rm.Create(new IdentityRole("Hirer")); // This role is used exclusively for the online hiring interface
var um = new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(DB));
var user = new ApplicationUser()
{
UserName = "jadmin",
IsApproved = true,
Email = "[email protected]"
};
ir = um.Create(user, "ChangeMe");
ir = um.AddToRole(user.Id, "Administrator"); //Default Administrator, edit to change
ir = um.AddToRole(user.Id, "Teacher"); //Required to make tests work
DB.Commit();
}
示例2: InitDefaultUsersAndRoles
private void InitDefaultUsersAndRoles(UserManager<QuestionsAnswersUser> userManager,
RoleManager<IdentityRole> roleManager)
{
if (roleManager.FindByName("admin") == null)
{
roleManager.Create(new IdentityRole("admin"));
}
if (roleManager.FindByName("user") == null)
{
roleManager.Create(new IdentityRole("user"));
}
if (userManager.FindByName("admin") == null)
{
var user = new QuestionsAnswersUser {UserName = "admin"};
var result = userManager.Create(user, "adminadmin");
if (result.Succeeded)
{
userManager.AddToRole(user.Id, "admin");
}
}
userManager.Users.Where(u => !u.Roles.Any()).ToList().ForEach(u => userManager.AddToRole(u.Id, "user"));
}
示例3: AddPermisionToADM
private void AddPermisionToADM(ApplicationDbContext db)
{
var userManarge = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
var user = userManarge.FindByName("[email protected]");
if (!userManarge.IsInRole(user.Id, "View"))
{
userManarge.AddToRole(user.Id, "View");
}
if (!userManarge.IsInRole(user.Id, "Create"))
{
userManarge.AddToRole(user.Id, "Create");
}
if (!userManarge.IsInRole(user.Id, "Edit"))
{
userManarge.AddToRole(user.Id, "Edit");
}
if (!userManarge.IsInRole(user.Id, "Delete"))
{
userManarge.AddToRole(user.Id, "Delete");
}
if (!userManarge.IsInRole(user.Id, "Adm"))
{
userManarge.AddToRole(user.Id, "Adm");
}
}
示例4: InitializeRoles
/// <summary>
/// Checks for the three roles - Admin, Employee and Complainant and
/// creates them if not present
/// </summary>
public static void InitializeRoles()
{ // Access the application context and create result variables.
ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdUserResult;
// Create a RoleStore object by using the ApplicationDbContext object.
// The RoleStore is only allowed to contain IdentityRole objects.
var roleStore = new RoleStore<IdentityRole>(context);
RoleManager roleMgr = new RoleManager();
if (!roleMgr.RoleExists("Administrator"))
{
roleMgr.Create(new ApplicationRole { Name = "Administrator" });
}
if (!roleMgr.RoleExists("Employee"))
{
roleMgr.Create(new ApplicationRole { Name = "Employee" });
}
if (!roleMgr.RoleExists("Complainant"))
{
roleMgr.Create(new ApplicationRole { Name = "Complainant" });
}
if (!roleMgr.RoleExists("Auditor"))
{
roleMgr.Create(new ApplicationRole { Name = "Auditor" });
}
// Create a UserManager object based on the UserStore object and the ApplicationDbContext
// object. Note that you can create new objects and use them as parameters in
// a single line of code, rather than using multiple lines of code, as you did
// for the RoleManager object.
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
UserName = "Administrator",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Admin123");
// If the new "canEdit" user was successfully created,
// add the "canEdit" user to the "canEdit" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "Administrator"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "Administrator");
}
appUser = new ApplicationUser
{
UserName = "Auditor",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Auditor123");
// If the new "canEdit" user was successfully created,
// add the "canEdit" user to the "canEdit" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "Auditor"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "Auditor");
}
}
示例5: SeedAdmin
internal static void SeedAdmin(TechSupportDbContext context)
{
const string AdminEmail = "[email protected]";
const string AdminPassword = "qweqwe";
if (context.Users.Any(u => u.Email == AdminEmail))
{
return;
}
var userManager = new UserManager<User>(new UserStore<User>(context));
var admin = new User
{
FirstName = "Pesho",
LastName = "Admina",
Email = AdminEmail,
Address = "Sopot",
UserName = AdminEmail
};
userManager.Create(admin, AdminPassword);
userManager.AddToRole(admin.Id, GlobalConstants.AdminRole);
userManager.AddToRole(admin.Id, GlobalConstants.ModeratorRole);
userManager.AddToRole(admin.Id, GlobalConstants.DefaultRole);
context.SaveChanges();
}
示例6: SeedModerator
internal static void SeedModerator(TechSupportDbContext context)
{
const string moderatorEmail = "[email protected]";
const string mderatorPassword = "moderator123456";
if (context.Users.Any(u => u.Email == moderatorEmail))
{
return;
}
var userManager = new UserManager<User>(new UserStore<User>(context));
var admin = new User
{
FirstName = "Gosho",
LastName = "Moderatora",
Email = moderatorEmail,
Address = "Sopot",
UserName = moderatorEmail
};
userManager.Create(admin, mderatorPassword);
userManager.AddToRole(admin.Id, GlobalConstants.ModeratorRole);
userManager.AddToRole(admin.Id, GlobalConstants.DefaultRole);
context.SaveChanges();
}
示例7: SeedAdmin
internal static void SeedAdmin(SciHubDbContext context)
{
const string adminUserName = "theDecider666";
const string adminPassword = "deciderd";
if (context.Users.Any(u => u.UserName == adminUserName))
{
return;
}
var userManager = new UserManager<User>(new UserStore<User>(context));
var admin = new User
{
UserName = adminUserName,
Email = "[email protected]",
FirstName = "Admin",
LastName = "Adminos",
Avatar = UserDefaultPictureConstants.Female,
Gender = Gender.Female,
About = "I am the Decider!"
};
userManager.Create(admin, adminPassword);
userManager.AddToRole(admin.Id, UserRoleConstants.Admin);
userManager.AddToRole(admin.Id, UserRoleConstants.Default);
context.SaveChanges();
}
示例8: SeedAdmin
internal static void SeedAdmin(GiftBoxDbContext context)
{
const string AdminEmail = "[email protected]";
const string AdminPassword = "123456";
if (context.Users.Any(u => u.Email == AdminEmail))
{
return;
}
var userManager = new UserManager<User>(new UserStore<User>(context));
var admin = new User
{
FirstName = "Adrian",
UserRole = "Admin",
LastName = "Apostolov",
Email = AdminEmail,
UserName = "Adrian.Apostolov",
PhoneNumber = "0889972697",
ImageUrl = GlobalConstants.DefaultUserPicture,
};
userManager.Create(admin, AdminPassword);
userManager.AddToRole(admin.Id, GlobalConstants.AdminRole);
userManager.AddToRole(admin.Id, GlobalConstants.UserRole);
userManager.AddToRole(admin.Id, GlobalConstants.HomeAdministrator);
context.SaveChanges();
}
示例9: SetupRolesAndUsers
private static void SetupRolesAndUsers(DbContext context)
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
// add roles
if (!roleManager.RoleExists(Role.Guest.ToString()))
roleManager.Create(new IdentityRole(Role.Guest.ToString()));
if (!roleManager.RoleExists(Role.Supplier.ToString()))
roleManager.Create(new IdentityRole(Role.Supplier.ToString()));
if (!roleManager.RoleExists(Role.Deactivated.ToString()))
roleManager.Create(new IdentityRole(Role.Deactivated.ToString()));
if (!roleManager.RoleExists(Role.User.ToString()))
roleManager.Create(new IdentityRole(Role.User.ToString()));
var adminRole = roleManager.FindByName(Role.Admin.ToString());
if (adminRole == null)
{
adminRole = new IdentityRole(Role.Admin.ToString());
roleManager.Create(adminRole);
}
#if DEBUG
//add admin user
var admin = userManager.Find(Admin_User, Admin_Pass);
if (admin == null)
{
admin = new ApplicationUser
{
UserName = Admin_User,
Email = Admin_Mail,
EmailConfirmed = true
};
var result = userManager.Create(admin, Admin_Pass);
// TODO: verify returned IdentityResult
userManager.AddToRole(admin.Id, Role.Admin.ToString());
result = userManager.SetLockoutEnabled(admin.Id, false);
}
var rolesForUser = userManager.GetRoles(admin.Id);
if (!rolesForUser.Contains(adminRole.Name))
{
var result = userManager.AddToRole(admin.Id, adminRole.Name);
}
//add normal user
if (userManager.Find("[email protected]", "1q2w3e4r") == null)
{
var user = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]",
EmailConfirmed = true
};
userManager.Create(user, "1q2w3e4r");
// TODO: verify returned IdentityResult
userManager.AddToRole(user.Id, Role.User.ToString());
}
#endif
}
示例10: Seed
public static void Seed(ApplicationDbContext context)
{
UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(context);
UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);
RoleStore<Role> roleStore = new RoleStore<Role>(context);
RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);
if (!roleManager.RoleExists("Admin"))
roleManager.Create(new Role { Name = "Admin" });
if (!roleManager.RoleExists("User"))
roleManager.Create(new Role { Name = "User" });
IdentityResult result = null;
ApplicationUser user1 = userManager.FindByName("[email protected]");
if (user1 == null)
{
user1 = new ApplicationUser { Email = "[email protected]", UserName = "[email protected]" };
}
result = userManager.Create(user1, "asdfasdf");
if (!result.Succeeded)
{
string error = result.Errors.FirstOrDefault();
throw new Exception(error);
}
userManager.AddToRole(user1.Id, "Admin");
user1 = userManager.FindByName("[email protected]");
ApplicationUser user2 = userManager.FindByName("[email protected]");
if (user2 == null)
{
user2 = new ApplicationUser { Email = "[email protected]", UserName = "[email protected]" };
}
result = userManager.Create(user2, "asdfasfd");
if (!result.Succeeded)
{
string error = result.Errors.FirstOrDefault();
throw new Exception(error);
}
userManager.AddToRole(user2.Id, "User");
user2 = userManager.FindByName("[email protected]");
}
示例11: CheckSuperUser
//Método para quemar el super usuario del sistema:
private void CheckSuperUser()
{
//me conecto all db:
var userContext = new ApplicationDbContext();
//Aqui controlo los usuarios:
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(userContext));
var db = new DemocracyContext();
this.CheckRole("Admin", userContext);
this.CheckRole("User", userContext);
//validar si el usuario existe en la tabla user:
var user = db.Users.Where(u=>u.userName.ToLower().Equals("[email protected]")).FirstOrDefault();
if (user == null)
{
//creamos nuestro usuario Administrador:
user = new User
{
Address = "Calle Madrid 55",
FirstName = "Emilio",
LastName = "Barrera",
Phone = "693661995",
userName = "[email protected]",
Photo = "~/Content/Photos/fondo.jpg"
};
db.Users.Add(user);
db.SaveChanges();
}
//valido si el uusarip existe en las tablas de the ASP NET User:
var userASP = userManager.FindByName(user.userName);
if (userASP == null)
{
userASP = new ApplicationUser
{
UserName = user.userName,
Email = user.userName,
PhoneNumber = user.Phone
};
userManager.Create(userASP, "Eabs+++++55555");
userManager.AddToRole(userASP.Id, "Admin");
}
userManager.AddToRole(userASP.Id, "Admin");
}
示例12: SeedRoles
public static void SeedRoles()
{
var context = new ApplicationDbContext();
if (!context.Roles.Any())
{
if (!context.Roles.Any())
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var roleCreateResult = roleManager.Create(new IdentityRole("Admin"));
if (!roleCreateResult.Succeeded)
{
throw new Exception(string.Join("; ", roleCreateResult.Errors));
}
var roleStore = new RoleStore<IdentityRole>(context);
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var user = new ApplicationUser() { UserName = "[email protected]", Email = "[email protected]" };
var createResult = userManager.Create(user, "123456");
if (!createResult.Succeeded)
{
throw new Exception(string.Join("; ", createResult.Errors));
}
userManager.AddToRole(user.Id, "admin");
context.SaveChanges();
}
}
}
示例13: AddUserAndRole
internal void AddUserAndRole()
{
// access the application context and create result variables.
Models.ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
// create roleStore object that can only contain IdentityRole objects by using the ApplicationDbContext object.
var roleStore = new RoleStore<IdentityRole>(context);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
// create admin role if it doesn't already exist
if (!roleMgr.RoleExists("admin"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "admin" });
}
// create a UserManager object based on the UserStore object and the ApplicationDbContext object.
// defines admin email account
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Pa$$word1");
// If the new admin user was successfully created, add the new user to the "admin" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "admin"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "admin");
}
}
示例14: CreateUserByRole
public static void CreateUserByRole(RoleEnum role, ApplicationUser user, string password, ApplicationDbContext db)
{
user.Id = Guid.NewGuid().ToString();
var store = new UserStore<ApplicationUser>(db);
var manager = new UserManager<ApplicationUser>(store);
using (var dbContextTransaction = db.Database.BeginTransaction())
{
try
{
manager.Create(user, password);
db.SaveChanges();
manager.AddToRole(user.Id, Enum.GetName(typeof(RoleEnum), (int)role));
db.SaveChanges();
dbContextTransaction.Commit();
}
catch (Exception)
{
dbContextTransaction.Rollback();
}
}
}
示例15: Create
public ActionResult Create(ClientViewModel client)
{
if (!ModelState.IsValid) return View();
//Register user and SingIn
var accountController = new AccountController {ControllerContext = this.ControllerContext};
var user = accountController.RegisterAccount(new RegisterViewModel() { Email = client.Email, Password = client.Password });
accountController.SignInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
//Add user to client role
var userStore = new UserStore<ApplicationUser>(_context);
var userManager = new UserManager<ApplicationUser>(userStore);
var roleStore = new RoleStore<IdentityRole>(_context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
if (!roleManager.RoleExists("Client"))
roleManager.Create(new IdentityRole("Client"));
userManager.AddToRole(user.Id, "Client");
//Register client
if (string.IsNullOrWhiteSpace(user.Id)) return View();
_context.Clients.Add(new Client()
{
Id = user.Id,
Name = client.Name,
Age = client.Age
});
_context.SaveChanges();
return RedirectToAction("Index", "Home");
}