示例#1
0
        public IHttpActionResult DeleteUser(string userEmail)
        {
            if (ModelState.IsValid && !string.IsNullOrEmpty(userEmail))
            {
                // Requests database to get user details for provided email
                using (Token_Based_Authentication_Web_APIEntities _entities = new Token_Based_Authentication_Web_APIEntities())
                {
                    AspNetUser user = _entities.AspNetUsers.Where(x => x.Email == userEmail).FirstOrDefault();

                    if (user == null)
                    {
                        // If user not found return with error
                        return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "User not found!")));
                    }

                    // If user found delete that user
                    try
                    {
                        _entities.AspNetUsers.Remove(user);
                        _entities.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        // If any exception happens then return the Server error with exception
                        return(InternalServerError(ex));
                    }
                }

                // Successful Message
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.OK, "User deleted successfully!")));
            }

            // If we got this far, something failed, redisplay form
            return(BadRequest(ModelState));
        }
        public async Task <IHttpActionResult> ForgotPassword(ForgotPasswordModel model)
        {
            if (ModelState.IsValid && model != null)
            {
                // Requests database for to get user details for provided email address
                using (Token_Based_Authentication_Web_APIEntities _entities = new Token_Based_Authentication_Web_APIEntities())
                {
                    AspNetUser user = _entities.AspNetUsers.Where(x => x.Email == model.Email).FirstOrDefault();

                    // If user not found return with error
                    if (user == null)
                    {
                        return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "User not found!")));
                    }

                    // Password Reset Token Generation
                    string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

                    // URL with password Token for resetting Password
                    string routeUrl = $"{HttpContext.Current.Request.Url.Scheme}://{Request.GetOwinContext().Request.Host.Value}/api/Account/ResetPassword?resetCode={code}";

                    // Saving Password in database for checking on the time of resetting password
                    user.ResetPasswordCode = code;
                    _entities.SaveChanges();

                    return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.OK, $"A password reset code is generated. Please user the following link to reset password. Link : { routeUrl }")));
                }
            }

            // If we got this far, something failed, redisplay form
            return(BadRequest(ModelState));
        }
        public IHttpActionResult ResetPassword(string resetCode, string newPassword)
        {
            if (ModelState.IsValid && !string.IsNullOrEmpty(resetCode) && !string.IsNullOrEmpty(newPassword))
            {
                // Requests database for to get user details for provided email address
                using (Token_Based_Authentication_Web_APIEntities _entities = new Token_Based_Authentication_Web_APIEntities())
                {
                    var user = _entities.AspNetUsers.Where(a => a.ResetPasswordCode == resetCode).FirstOrDefault();

                    // If user not found return with error
                    if (user == null)
                    {
                        return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "User not found!")));
                    }

                    // If user found then reset the password with new provided password
                    if (user != null)
                    {
                        user.PasswordHash      = Crypto.Hash(newPassword);
                        user.ResetPasswordCode = string.Empty;
                        _entities.SaveChanges();

                        return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.OK, "Password reset successful!")));
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return(BadRequest(ModelState));
        }
示例#4
0
        public IHttpActionResult GetUserDetails(string userEmail)
        {
            if (ModelState.IsValid && !string.IsNullOrEmpty(userEmail))
            {
                // Requests database to get user details for provided email and checks it's authnticity
                using (Token_Based_Authentication_Web_APIEntities _entities = new Token_Based_Authentication_Web_APIEntities())
                {
                    AspNetUser user = _entities.AspNetUsers.Where(x => x.Email == userEmail).FirstOrDefault();

                    if (user == null)
                    {
                        // If user not found return with error
                        return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "User not found!")));
                    }

                    // If user found return the authentic user message
                    return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.OK, "User exist with provided email")));
                }
            }

            // If we got this far, something failed, redisplay form
            return(BadRequest(ModelState));
        }