Example #1
0
        public async Task <IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var info = await Authentication.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(InternalServerError());
            }

            var user = new ISS.Authentication.Domain.Models.User()
            {
                UserName = model.Email, Email = model.Email
            };

            IdentityResult result = await UserManager.CreateAsync(user);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            result = await UserManager.AddLoginAsync(user.Id, info.Login);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }
            return(Ok());
        }
Example #2
0
        public async Task <IHttpActionResult> GetExternalLogin(string provider, string error = null)
        {
            if (error != null)
            {
                return(Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error)));
            }

            if (!User.Identity.IsAuthenticated)
            {
                return(new ChallengeResult(provider, this));
            }

            ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

            if (externalLogin == null)
            {
                return(InternalServerError());
            }

            if (externalLogin.LoginProvider != provider)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                return(new ChallengeResult(provider, this));
            }

            ISS.Authentication.Domain.Models.User user = await UserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider,
                                                                                                       externalLogin.ProviderKey));

            bool hasRegistered = user != null;

            if (hasRegistered)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);

                ClaimsIdentity oAuthIdentity = await UserManager.GenerateUserIdentityAsync(user,
                                                                                           OAuthDefaults.AuthenticationType);

                ClaimsIdentity cookieIdentity = await UserManager.GenerateUserIdentityAsync(user,
                                                                                            CookieAuthenticationDefaults.AuthenticationType);

                AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName);
                Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);
            }
            else
            {
                IEnumerable <Claim> claims   = externalLogin.GetClaims();
                ClaimsIdentity      identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);
                Authentication.SignIn(identity);
            }

            return(Ok());
        }
Example #3
0
        public async Task <IHttpActionResult> PasswordReset(ResetPasswordBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            PasswordResetToken _token = await UnitOfWork.PasswordResetTokenStore.FindByToken(model.ResetToken);

            if (_token == null)
            {
                return(BadRequest("Invalid Token"));
            }
            if (_token.Expires < DateTime.Now)
            {
                return(BadRequest("Expired Token"));
            }
            if (_token.Used.HasValue)
            {
                return(BadRequest("Token Already Used"));
            }
            ISS.Authentication.Domain.Models.User _user = await UnitOfWork.UserStore.FindByIdAsync(_token.UserId);

            if (_user == null)
            {
                return(BadRequest("User not Found"));
            }
            IdentityResult _result = await UserManager.RemovePasswordAsync(_user.Id);

            _result = await UserManager.AddPasswordAsync(_user.Id, model.NewPassword);

            _token.Used = DateTime.Now;
            if (_result.Succeeded)
            {
                await UnitOfWork.PasswordResetTokenStore.UpdateAsync(_token);

                return(Ok());
            }
            else
            {
                string _errors = "";
                foreach (string _error in _result.Errors)
                {
                    if (_errors != "")
                    {
                        _errors += "; ";
                    }
                    _errors += _error;
                }
                return(BadRequest(_errors));
            }
        }
Example #4
0
        public async Task <IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = new ISS.Authentication.Domain.Models.User()
            {
                UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName
            };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            return(Ok());
        }
Example #5
0
        public async Task <IHttpActionResult> PasswordReminder(ForgottenPasswordBindingModel model)
        {
            ISS.Authentication.Domain.Models.User _user = await UnitOfWork.UserStore.FindByEmailAsync(model.Email);

            List <ISS.Authentication.Domain.Models.User> _users = await UnitOfWork.UserStore.ListAsync();

            if (_user == null)
            {
                return(BadRequest());
            }
            else
            {
                //ISS.Authentication.Domain.Models.EmailTemplate _template = await UnitOfWork.EmailTemplateStore.FindByIdAsync(NullHandlers.NGUID(ConfigurationManager.AppSettings["passwordReminderTemplateId"]));
                //if (_template == null)
                //{
                //    return InternalServerError();
                //}

                ISS.Authentication.Domain.Models.PasswordResetToken _token = await UnitOfWork.PasswordResetTokenStore.CreateAsync(_user.Id, 60);

                if (_token == null)
                {
                    return(InternalServerError());
                }

                //string _body = _template.Body.Replace("[[Token]]", _token.Token).Replace("[[User.FirstName]]", _user.FirstName);

                //List<string> _to = new List<string>();
                //_to.Add(model.Email);
                //if (await EmailService.SendEmail(_template.Subject, _body, _template.From, _to, new List<string>(), new List<string>(), new List<string>()))
                //{
                return(Ok());
                //}
                //else
                //{
                //    return InternalServerError();
                //}
            }
        }
Example #6
0
        public async Task <IHttpActionResult> UpdateAccount(UpdateAccountBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            ISS.Authentication.Domain.Models.User user = await UserManager.FindByIdAsync(new Guid(User.Identity.GetUserId()));

            if ((model.Email != user.Email) && (model.Password != null) && (model.Password.Trim() != ""))
            {
                if (await UserManager.CheckPasswordAsync(user, model.Password) == true)
                {
                    user.Email          = model.Email;
                    user.UserName       = user.Email;
                    user.EmailConfirmed = false;
                }
                else
                {
                    return(BadRequest("The provided password was incorrect"));
                }
            }
            else
            {
                return(BadRequest("You must provide your password in order to change your email address"));
            }
            user.FirstName = model.FirstName;
            user.LastName  = model.LastName;
            IdentityResult result = UserManager.Update(user);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            return(Ok());
        }