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 = "******", IsApproved = true, Email = "*****@*****.**" }; 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(); }
// Allow Initialization with an instance of ApplicationUser: public EditUserViewModel(ApplicationUser user) { this.UserName = user.UserName; string[] firstLast = user.UserName.Split('.'); if (firstLast.Length == 2) { this.FirstName = firstLast[0]; this.LastName = firstLast[1]; } else { this.FirstName = user.UserName; this.LastName = ""; } this.Email = user.Email; this.IsApproved = user.IsApproved; this.IsLockedOut = user.IsLockedOut; }
// Allow Initialization with an instance of ApplicationUser: public EditUserViewModel(ApplicationUser user) { // TODO: add logic here to only retrieve first/last name if not hirer this.UserName = user.UserName; string[] firstLast = user.UserName.Split('.'); if (firstLast.Length == 2) { this.FirstName = firstLast[0]; this.LastName = firstLast[1]; } else { this.FirstName = user.UserName; this.LastName = ""; } this.Email = user.Email; this.IsApproved = user.IsApproved; this.IsLockedOut = user.IsLockedOut; this.Id = user.Id; }
private bool HasPassword(ApplicationUser user) { // Return whether user has a password hash return (user.PasswordHash != null); }
private bool IsInternalUser(ApplicationUser user) { // Return whether user has a password hash return (user.UserName.Contains("@")); }
private async Task SignInAsync(ApplicationUser user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); }
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return RedirectToAction("Manage"); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } var user = new ApplicationUser() { UserName = model.FirstName + "." + model.LastName }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { string newUserName = model.FirstName.Trim() + "." + model.LastName.Trim(); ApplicationUser user = new ApplicationUser() { UserName = newUserName, LoweredUserName = newUserName.ToLower(), ApplicationId = GetApplicationID(), Email = model.Email.Trim(), LoweredEmail = model.Email.Trim() }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // TODO: add user role to user & sign them in // TODO: provide messaging to administrator to add appropriate roles to their account //await SignInAsync(user, isPersistent: false); return RedirectToAction("Index", "Home"); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return View(model); }
// Enable initialization with an instance of ApplicationUser: public SelectUserRolesViewModel(ApplicationUser user, IDatabaseFactory dbFactory) : this() { this.UserName = user.UserName; string[] firstLast = user.UserName.Split('.'); this.FirstName = firstLast[0]; if (firstLast.Length > 1) { this.LastName = firstLast[1]; } else { this.LastName = ""; } this.UserId = user.Id; // Add all available roles to the list of EditorViewModels: IDbSet<IdentityRole> allRoles = dbFactory.Get().Roles; foreach (var role in allRoles) { // An EditorViewModel will be used by Editor Template: var rvm = new SelectRoleEditorViewModel(role); this.Roles.Add(rvm); } // Set the Selected property to true for those roles for // which the current user is a member: foreach (IdentityUserRole userRole in user.Roles) { SelectRoleEditorViewModel checkUserRole = this.Roles.Find(r => r.RoleName == userRole.Role.Name); checkUserRole.Selected = true; } }
// Return a pre-populated instance of ApplicationUser: public ApplicationUser GetUser() { var user = new ApplicationUser() { UserName = this.FirstName.Trim() + "." + this.LastName.Trim(), Email = this.Email.Trim(), }; return user; }
// Return a pre-populated instance of ApplicationUser: public ApplicationUser GetUser() { var user = new ApplicationUser() { UserName = this.Email, Email = this.Email, }; return user; }
private bool HasPassword(ApplicationUser user) { if (user.PasswordHash != null) { return true; } return false; }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var currentApplicationId = GetApplicationID(); string newUserName = model.FirstName.Trim() + "." + model.LastName.Trim(); var user = new ApplicationUser() { UserName = newUserName, LoweredUserName = newUserName.ToLower(), ApplicationId=currentApplicationId }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //await SignInAsync(user, isPersistent: false); return RedirectToAction("Index", "Home"); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return View(model); }
public async Task<ActionResult> Register(HirerRegisterViewModel model) { if (ModelState.IsValid) { // Initialize user object ApplicationUser user = new ApplicationUser() { UserName = model.Email.ToLower().Trim(), LoweredUserName = model.Email.Trim().ToLower(), ApplicationId = GetApplicationID(), Email = model.Email.Trim(), LoweredEmail = model.Email.Trim().ToLower() }; MacheteContext Db = DatabaseFactory.Get(); // Create user IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // Retrieve newly created user record to retrieve userId ApplicationUser newUser = await UserManager.FindAsync(user.UserName, model.Password); if (newUser != null) { result = await UserManager.AddToRoleAsync(newUser.Id, "Hirer"); if (result.Succeeded) { // Sign in user await SignInAsync(user, isPersistent: false); // Redirect to hire worker page return RedirectToAction("/", "HirerWorkOrder"); } else { AddErrors(result); } } else // new user couldn't be found { // TODO: provide error reporting } } else // create new user failed { // Message the user AddErrors(result); } } // If we got this far, something failed, redisplay form return View(model); }