Exemple #1
0
        public async Task <ActionResult> ForgotPassword([FromBody] ForgotPasswordModel data)
        {
            try
            {
                string OTP;
                var    checkUser = MH.CheckForDatas("UserName", data.UserName, null, null, "Authentication", "Authentication");
                var    filter    = Builders <BsonDocument> .Filter.Eq("UserName", data.UserName);

                var user = MH.GetSingleObject(filter, "Authentication", "Authentication").Result;
                if (user != null)
                {
                    var userData = BsonSerializer.Deserialize <RegisterModel>(user);
                    VerificationModel smsModel = new VerificationModel();
                    smsModel.UserName = data.UserName;
                    if (data.UserLocation == "IN")
                    {
                        Random codeGenerator = new Random();
                        OTP = codeGenerator.Next(0, 1000000).ToString("D6");
                        smsModel.VerificationCode = OTP;
                        SMSHelper.SendSMS(data.UserName, OTP);
                    }
                    else
                    {
                        OTP = Guid.NewGuid().ToString();
                        string link = GlobalHelper.GetIpConfig() + data.UserName + "/" + OTP + "/yes";
                        await EmailHelper.SendEmail(userData.FullName, data.UserName, link);
                    }
                    var update = Builders <BsonDocument> .Update.Set("Status", "Not Verified").Set("OTPExp", DateTime.UtcNow.AddMinutes(2))
                                 .Set("VerificationCode", smsHasher.HashPassword(smsModel, OTP));

                    var result = MH.UpdateSingleObject(filter, "Authentication", "Authentication", update).Result;
                    return(Ok(new ResponseData
                    {
                        Code = "200",
                        Message = "Success",
                        Data = null
                    }));
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "User not found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("AuthController", "ForgetPassword", "ForgetPassword", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = null
                }));
            }
        }
Exemple #2
0
        public ActionResult ChangePassword([FromBody] LoginModel data)
        {
            try
            {
                var filter = Builders <BsonDocument> .Filter.Eq("UserName", data.UserName);

                var user = MH.GetSingleObject(filter, "Authentication", "Authentication").Result;
                if (user != null)
                {
                    var verifyUser = BsonSerializer.Deserialize <RegisterModel>(user);
                    if (verifyUser.Status == "Verified")
                    {
                        RegisterModel registerModel = new RegisterModel {
                            UserName = data.UserName, Password = data.Password
                        };
                        var update = Builders <BsonDocument> .Update.Set("Password", passwordHasher.HashPassword(registerModel, data.Password));

                        var result = MH.UpdateSingleObject(filter, "Authentication", "Authentication", update).Result;
                        return(Ok(new ResponseData
                        {
                            Code = "200",
                            Message = "Password Changed Successfully",
                            Data = null
                        }));
                    }
                    else
                    {
                        return(BadRequest(new ResponseData
                        {
                            Code = "401",
                            Message = "User Not Verified to Change Password",
                            Data = null
                        }));
                    }
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "User Not Found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("AuthController", "ChangePassword", "ChangePassword", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = null
                }));
            }
        }
Exemple #3
0
        public ActionResult ChangePasswordWhenLoggedIn([FromBody] ChangePasswordModel data)
        {
            try
            {
                var filter = Builders <BsonDocument> .Filter.Eq("UserName", data.UserName);

                var user = MH.GetSingleObject(filter, "Authentication", "Authentication").Result;
                if (user != null)
                {
                    var           verifyUser    = BsonSerializer.Deserialize <RegisterModel>(user);
                    RegisterModel registerModel = new RegisterModel {
                        UserName = verifyUser.UserName, Password = data.OldPassword
                    };
                    if (passwordHasher.VerifyHashedPassword(registerModel, verifyUser.Password, data.OldPassword).ToString() == "Success")
                    {
                        var update = Builders <BsonDocument> .Update.Set("Password", passwordHasher.HashPassword(verifyUser, data.NewPassword));

                        var result = MH.UpdateSingleObject(filter, "Authentication", "Authentication", update).Result;
                        return(Ok(new ResponseData
                        {
                            Code = "200",
                            Message = "Password Changed Successfully",
                            Data = null
                        }));
                    }
                    else
                    {
                        string response = GlobalHelper.RecordLoginAttempts(filter);
                        if (response != "Failed")
                        {
                            return(BadRequest(new ResponseData
                            {
                                Code = "401",
                                Message = "Invalid User Infomation",
                                Data = null
                            }));
                        }
                        else
                        {
                            return(BadRequest(new ResponseData
                            {
                                Code = "400",
                                Message = "Failed",
                                Data = null
                            }));
                        }
                    }
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "User Not Found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("AuthController", "ChangePasswordWhenLoggedIn", "ChangePasswordWhenLoggedIn", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = null
                }));
            }
        }
Exemple #4
0
        public ActionResult ForgotPasswordVerification(string username, string otp)
        {
            try
            {
                var filter = Builders <BsonDocument> .Filter.Eq("UserName", username);

                var user = MH.GetSingleObject(filter, "Authentication", "Authentication").Result;
                if (user != null)
                {
                    var verifyUser = BsonSerializer.Deserialize <RegisterModel>(user);
                    if (verifyUser.OTPExp > DateTime.UtcNow)
                    {
                        VerificationModel model = new VerificationModel {
                            UserName = username, VerificationCode = otp
                        };
                        if (smsHasher.VerifyHashedPassword(model, verifyUser.VerificationCode, otp).ToString() == "Success")
                        {
                            var update = Builders <BsonDocument> .Update.Set("Status", "Verified");

                            var result = MH.UpdateSingleObject(filter, "Authentication", "Authentication", update).Result; Parameters parameters = new Parameters();
                            parameters.username = username;
                            parameters.fullname = verifyUser.FullName;
                            var response = authHelper.DoPassword(parameters, _repo, _settings);
                            response.Code    = "201";
                            response.Message = "OTP Verified";
                            return(Ok(Json(response)));
                        }
                        else
                        {
                            return(BadRequest(new ResponseData
                            {
                                Code = "401",
                                Message = "Invalied OTP",
                                Data = null
                            }));
                        }
                    }
                    else
                    {
                        return(BadRequest(new ResponseData
                        {
                            Code = "402",
                            Message = "OTP Expired",
                            Data = null
                        }));
                    }
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "User Not Found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("AuthController", "ForgotPasswordVerification", "ForgotPasswordVerification", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = null
                }));
            }
        }