Пример #1
0
        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)));
        }
Пример #2
0
 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;
 }
Пример #3
0
        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)));
        }
Пример #4
0
        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);
        }
Пример #5
0
        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));
        }