protected void CreateUser_Click(object sender, EventArgs e) { // Default UserStore constructor uses the default connection string named: DefaultConnection var context = StudentsManagerDbContext.GetInstance(); var userStore = new ApplicationUserStore <ApplicationUser>(context); var manager = new ApplicationUserManager <ApplicationUser>(); var user = new ApplicationUser() { UserName = UserName.Text }; IdentityResult result = manager.Create(user, Password.Text); if (result.Succeeded) { StatusMessage.Text = string.Format("User {0} was created successfully!", user.UserName); } else { StatusMessage.Text = result.Errors.FirstOrDefault(); } ApplicationUser someUser = context.Users.FirstOrDefault(); context.Tasks.Add(new Task { Desciption = "You lold", Level = 9, Name = "lo", Tags = "Oh, my, tags" }); context.SaveChanges(); }
public ApplicationUserManager() : base(new ApplicationUserStore <TUser>(StudentsManagerDbContext.GetInstance())) { this.UserValidator = new UserValidator <TUser>(this) { AllowOnlyAlphanumericUserNames = false }; }
public string GetRoleName(string username, string passwordHash) { var context = StudentsManagerDbContext.GetInstance(); var userStore = new ApplicationUserStore <ApplicationUser>(context); var userManager = new ApplicationUserManager <ApplicationUser>(); var userByName = userManager.FindByName(username); if (userByName == null) { return("None"); } string roleName = userManager .GetRoles <ApplicationUser, string>(userByName.Id) .FirstOrDefault(); return(roleName); }
public LoginResult Login(string username, string passwordHash) { var result = new LoginResult(); var userStore = new ApplicationUserStore <ApplicationUser>(StudentsManagerDbContext.GetInstance()); var userManager = new ApplicationUserManager <ApplicationUser>(); var userByName = userManager.FindByName(username); ApplicationUser userByPassword = null; if (userByName != null) { userByPassword = userManager.Find(username, passwordHash); } if (userByPassword != null) { var authenticationManager = HttpContext.Current.GetOwinContext().Authentication; authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = userManager.CreateIdentity <ApplicationUser, string>(userByPassword, DefaultAuthenticationTypes.ApplicationCookie); //var identity = await userManager.CreateIdentityAsync(userByPassword, DefaultAuthenticationTypes.ApplicationCookie); authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity); result.message = "ok"; result.authenticationResult = true; result.userName = userByPassword.UserName; result.userRole = userManager.IsInRole(userByPassword.Id, "Student") ? "Student" : "Teacher"; } else { result.message = "Invalid email or password"; result.authenticationResult = false; } return(result); }
public ApplicationUserStore(StudentsManagerDbContext dbContext) : base(dbContext) { }