public IEnumerable <string> CreateStudent(AppUser user, IAuthenticationManager manager, AppIdentityDbContext db) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(db)); UserRoleManager roleMgr = new UserRoleManager(new RoleStore <UserRoles>(db)); IdentityResult res = userMgr.Create(user, user.Password); userMgr.CheckPassword(user, user.Password); if (res.Succeeded && roleMgr.RoleExists("student")) { IdentityResult role = userMgr.AddToRole(user.Id, "student"); db.Students.Add(new Student { StudentName = user.UserName, }); ClaimsIdentity identity = userMgr.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); manager.SignOut(); manager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity); return(null); } else { return(res.Errors); } }
public IHttpActionResult Post([FromBody] LoginModel loginModel) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(db)); var model = FindAppUser(loginModel.Login); if (userMgr.CheckPassword(model, loginModel.Password)) { return(Json(new UserModelView(model))); } else { var message = "Неверный логин или пароль"; HttpError err = new HttpError(message); return(ResponseMessage(Request.CreateResponse(HttpStatusCode.Unauthorized, err))); } }
public IEnumerable <string> CreateUser(AppUser user, string password, IAuthenticationManager manager) { AppUserManager userMgr = new AppUserManager(new UserStore <AppUser>(db)); AppRoleManager roleMgr = new AppRoleManager(new RoleStore <UserRoles>(db)); IdentityResult res = userMgr.Create(user, password); userMgr.CheckPassword(user, password); if (password.Contains("12345")) { List <string> errors = res.Errors.ToList(); errors.Add("Пароль не должен содержать последовательность чисел"); return(errors); } else if (res.Succeeded && roleMgr.RoleExists("user")) { IdentityResult role = userMgr.AddToRole(user.Id, "user"); db.Accounts.Add(new Account { Id = user.Id, UserName = user.UserName, Email = user.Email, PhoneNumber = user.PhoneNumber }); ClaimsIdentity identity = userMgr.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); manager.SignOut(); manager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity); return(null); } else { return(res.Errors); } }
public IHttpActionResult Login(LoginModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = AppUserManager.FindByName(model.Username); if (user == null) { return(BadRequest(string.Format("User {0} does not exist.", model.Username))); } if (!AppUserManager.CheckPassword(user, model.Password)) { return(BadRequest("Invalid password.")); } var identity = AppUserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); var auth = Request.GetOwinContext().Authentication; auth.SignIn(identity); return(Ok()); }