示例#1
0
        public async Task <IHttpActionResult> GetResetPassword(string UserName)
        {
            User         discuser;
            IdentityUser _user = await UserManager.FindByNameAsync(UserName);

            if (_user == null)
            {
                ModelState.AddModelError("", "UserName Not Found");
                return(BadRequest(ModelState));
            }
            else
            {
                try
                {
                    discuser = await QueryWorker.GetUserByUserName(UserName);
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", e.Message);
                    return(BadRequest(ModelState));
                }
            }

            if (discuser == null)
            {
                ModelState.AddModelError("", "UserName Not Found");
                return(BadRequest(ModelState));
            }

            //1° modo:

            /*
             * string hashedNewPassword = UserManager.PasswordHasher.HashPassword("prova");
             * UserStore<IdentityUser> store = new UserStore<IdentityUser>(db);
             * await store.SetPasswordHashAsync(_user, hashedNewPassword);
             * await store.UpdateAsync(_user);
             */

            // 2° modo
            //string npwd = Guid.NewGuid().ToString("d").Substring(1, 8);
            string npwd = Membership.GeneratePassword(12, 0);

            UserManager.RemovePassword(_user.Id);
            UserManager.AddPassword(_user.Id, npwd);

            try
            {
                await MailProvider.GetMailprovider().SendForgottenPwdEmail(discuser.Email, npwd);

                return(Ok());
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
                return(BadRequest(ModelState));
            }
        }
示例#2
0
        public async Task <IHttpActionResult> Register(RegisterUserViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                CommandWorker.RegisterUser(model);

                // Create new Account
                IdentityUser user = new IdentityUser
                {
                    UserName = model.UserName
                };

                string            decryptedPwd = Codec.DecryptStringAES(model.Password);
                IdentityResult    result       = UserManager.Create(user, decryptedPwd);
                IHttpActionResult errorResult  = GetErrorResult(result);

                if (errorResult != null)
                {
                    return(errorResult);
                }

                await MailProvider.GetMailprovider().SendActivationEmail(model.Email, model.UserName, decryptedPwd, model.ActivationKey, Request.Headers.Referrer.AbsoluteUri);

                //TODO: Remove -> Register a user does NOT require to return a User (needs an activation step)
                User discuser = await QueryWorker.GetUserByUserName(model.UserName);

                return(Ok(discuser));
            }
            catch (Exception e)
            {
                return(BadDisciturRequest(e.Message));
            }
        }