本文整理汇总了C#中UserManager.Create方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.Create方法的具体用法?C# UserManager.Create怎么用?C# UserManager.Create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.Create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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");
}
}
示例2: SeedData
public void SeedData(EmployerEmployeeHuntDbContext context)
{
var userStore = new UserStore<User>(context);
var userManager = new UserManager<User>(userStore);
var adminUser = new User
{
UserName = AdministratorUserName,
Email = AdministratorUserName
};
var headhunterUser = new User
{
UserName = HeadhunterUserName,
Email = HeadhunterUserName
};
var developerUser = new User
{
UserName = DeveloperUserName,
Email = DeveloperUserName
};
var employerUser = new User
{
UserName = EmployerUserName,
Email = EmployerUserName
};
userManager.Create(adminUser, AdministratorPassword);
userManager.Create(headhunterUser, HeadhunterPassword);
userManager.Create(developerUser, DeveloperPassword);
userManager.Create(employerUser, EmployerPassword);
userManager.AddToRole(adminUser.Id, GlobalConstants.AdministratorRoleName);
userManager.AddToRole(headhunterUser.Id, GlobalConstants.HeadhunterRoleName);
userManager.AddToRole(developerUser.Id, GlobalConstants.UserRoleName);
userManager.AddToRole(employerUser.Id, GlobalConstants.UserRoleName);
for (int i = 0; i < 60; i++)
{
var currentUser = new User
{
Email = string.Format("user_{0}@somemail.com", i + 1),
UserName = string.Format("user_{0}", i + 1)
};
userManager.Create(currentUser, currentUser.Email);
if (i % 2 == 0)
{
userManager.AddToRole(currentUser.Id, GlobalConstants.HeadhunterRoleName);
}
else
{
userManager.AddToRole(currentUser.Id, GlobalConstants.UserRoleName);
}
}
}
示例3: 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
}
示例4: 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]");
}
示例5: CreateUser_Click
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)
{
ApplicationUser newUser = manager.Find(UserName.Text, Password.Text);
var sa = new StoredAccount();
sa.CreateNewAccount(newUser.Id,Email.Text);
var returnUrl = Request.QueryString["ReturnUrl"];
IdentityHelper.SignIn(manager, user, isPersistent: false);
if (returnUrl == null)
{
IdentityHelper.RedirectToReturnUrl("~/Game/User-Home.aspx", Response);
}
else
{
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
示例6: Doctors_Create
public ActionResult Doctors_Create([DataSourceRequest]DataSourceRequest request, DoctorGridViewModel doctor)
{
if (this.ModelState.IsValid)
{
//TODO:Create service for users
var context = new MyMedicalGuideDbContext();
var userManager = new UserManager<User>(new UserStore<User>(context));
var userDoctor = new User()
{
Email = doctor.Email,
FirstName = doctor.FirstName,
LastName = doctor.LastName,
PhoneNumber = doctor.PhoneNumber,
UserName = doctor.Username
};
var hospitalId = this.TempData["hospitalId"];
userManager.Create(userDoctor, doctor.Password);
var DoctorDb = new MyMedicalGuide.Data.Models.Doctor() { User = userDoctor, HospitalId = (int)hospitalId, DepartmentId = doctor.DepartmentId, CreatedOn = DateTime.Now };
context.Doctors.Add(DoctorDb);
userManager.AddToRole(userDoctor.Id, "Doctor");
}
return this.Json(new[] { doctor }.ToDataSourceResult(request, this.ModelState));
}
示例7: CreateUser_Click
protected void CreateUser_Click(object sender, EventArgs e)
{
// Default UserStore constructor uses the default connection string named: DefaultConnectionEF
var userStore = new UserStore<IdentityUser>();
var manager = new UserManager<IdentityUser>(userStore);
var user = new IdentityUser() { UserName = txtUName.Text };
user.Email = txtEmail.Text;
user.PhoneNumber = txtPhone.Text;
IdentityResult result = manager.Create(user, txtPass.Text);
if (result.Succeeded)
{
lblStatus.Text = string.Format("User {0} was created successfully!", user.UserName);
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
Response.Redirect("/admin/main.aspx");
}
else
{
lblStatus.Text = result.Errors.FirstOrDefault();
}
}
示例8: CreateUser
public static ApplicationUser CreateUser(UserManager<ApplicationUser> userManager, string email, string firstName, string lastName,
string password, bool lockOutEnabled)
{
var user = userManager.FindByName(email);
if (user == null)
{
user = new ApplicationUser
{
UserName = email,
Email = email,
FirstName = firstName,
LastName = lastName,
EmailConfirmed = true
};
try
{
userManager.Create(user, password);
}
catch (Exception ex)
{
Log4NetHelper.Log("Error creating Admin User", LogLevel.ERROR, "AspNetUser", 1, "none", ex);
}
userManager.SetLockoutEnabled(user.Id, lockOutEnabled);
}
return user;
}
示例9: btnSave_Click
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
// Default UserStore constructor uses the default connection string named: DefaultConnection
var userStore = new UserStore<IdentityUser>();
var manager = new UserManager<IdentityUser>(userStore);
var user = new IdentityUser() { UserName = txtUsername.Text };
IdentityResult result = manager.Create(user, txtPassword.Text);
if (result.Succeeded)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
Response.Redirect("/user/index.aspx");
}
else
{
lblStatus.Text = result.Errors.FirstOrDefault();
}
}
catch (Exception d)
{
Response.Redirect("/error.aspx");
}
}
示例10: RegisterButton_Click
protected void RegisterButton_Click(object sender, EventArgs e)
{
//crete new userStore and userManager objects
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var user = new IdentityUser()
{
UserName = UserNameTextBox.Text,
PhoneNumber = PhoneNumberTextBox.Text,
Email = EmailTextBox.Text
};
//create new user in the dbb and store the resukt
IdentityResult result = userManager.Create(user, PasswordTextBox.Text);
//check if succesfully registered
if (result.Succeeded)
{
//authenticate and login new user
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);//store info in session
//sign in
authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
//redirect to main menu
Response.Redirect("~/Secured/TodoList.aspx");
}
else
{
//display error in the AlertFlash div
StatusLabel.Text = result.Errors.FirstOrDefault();
AlertFlash.Visible = true;
}
}
示例11: 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();
}
示例12: 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();
}
示例13: AddUserAndRole
internal void AddUserAndRole()
{
Models.ApplicationDbContext context = new Models.ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
var roleStore = new RoleStore<IdentityRole>(context);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
if (!roleMgr.RoleExists("administrator"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "administrator" });
}
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
UserName = "administrator",
};
IdUserResult = userMgr.Create(appUser, "1qaz2wsxE");
var user = userMgr.FindByName("administrator");
if (!userMgr.IsInRole(user.Id, "administrator"))
{
//userMgr.RemoveFromRoles(user.Id, "read", "edit");
IdUserResult = userMgr.AddToRole(userMgr.FindByName("administrator").Id, "administrator");
}
}
示例14: 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();
}
示例15: CreateUser_Click
protected void CreateUser_Click(object sender, EventArgs e)
{
// Default UserStore constructor uses the default connection string named: DefaultConnection
var userStore = new UserStore<IdentityUser>();
var manager = new UserManager<IdentityUser>(userStore);
//IdentityResult IdUserResult;
var user = new IdentityUser() { UserName = UserName.Text };
IdentityResult result = manager.Create(user, Password.Text);
if (result.Succeeded)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
// IdUserResult = manager.AddToRole(manager.FindByName(user.UserName).Id, "member");
Session["uPass"] = Password.Text;
Response.Redirect("~/WebForm2.aspx");
// StatusMessage.Text = string.Format("User {0} was created successfully!", user.UserName);
}
else
{
StatusMessage.Text = result.Errors.FirstOrDefault();
}
}