public virtual async Task <IHttpActionResult> SaveAsync(User user, bool isNew) { if (isNew) { // PasswordHash must contains user password NOT hash. //user.Email = user.Email ?? string.Format("{0}@{0}.{0}", user.UserName); IdentityResult result = await UserManager.CreateAsync(user, user.PasswordHash); if (!result.Succeeded) { return(GetErrorResult(result)); } } else { var currentUser = Db.Users.Include(u => u.Employees).First(x => x.Id == user.Id); currentUser.UserName = user.UserName; currentUser.Email = user.Email; if (user.PasswordHash != null) { currentUser.PasswordHash = UserManager.PasswordHasher.HashPassword(user.PasswordHash); } await Db.SaveChangesAsync(); user = currentUser; } return(Ok(JsonUser.Create(user))); }
private TEntity PrepareResultEntity<TEntity>(object p) { var u = p as User; if (u != null) { var jsonUser = JsonUser.Create(u); if (typeof (TEntity) == typeof (JsonUser)) p = jsonUser; else p = jsonUser.ToUser(); } return (TEntity)p; }
public async Task <IHttpActionResult> Login(LoginBindingModel model) { //Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie); Authentication.SignOut(); var user = await UserManager.FindAsync(model.Login, model.Password); if (user == null) { return(GetErrorResult(new IdentityResult("The user name or password is incorrect."))); } var cookieIdentity = await user.GenerateUserIdentityAsync(UserManager, DefaultAuthenticationTypes.ApplicationCookie); var properties = ApplicationOAuthProvider.CreateProperties(user.UserName); properties.IsPersistent = model.RememberMe; Authentication.SignIn(properties, cookieIdentity); return(Ok(JsonUser.Create(user))); }
private TEntity PrepareResultEntity <TEntity>(object p, int level = 0) { var u = p as User; if (u != null) { var jsonUser = JsonUser.Create(u); if (level > 0) { jsonUser.Employees = null; } if (typeof(TEntity) == typeof(JsonUser)) { p = jsonUser; } else { p = jsonUser.ToUser(); } } return((TEntity)p); }
public async Task <JsonUser> GetAsync(string id) { var res = await Db.Set <User>().Include(u => u.Employees).FirstOrDefaultAsync(u => u.Id == id); return(JsonUser.Create(res)); }