using System; using System.Threading.Tasks; using System.Web.Mvc; using FHT.Agency.Services.ImplementedInterfaces; using FHT.Agency.Services.ImplementedInterfaces.Exceptions; using FHT.Agency.UI.InputModels; using Microsoft.AspNet.Identity; namespace FHT.Agency.Web.Controllers { public class AccountController : Controller { private readonly IAccountService _accountService; private const string Password = "123456"; public AccountController(IAccountService accountService) { _accountService = accountService; } [AllowAnonymous] public ActionResult Login(string returnUrl) { ViewBag.ReturnUrl = returnUrl; return View(); } [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task Login(LoginInputModel model, string returnUrl) { if (!ModelState.IsValid) { return View(model); } try { await _accountService.LoginAsync(model); if (returnUrl != null) { return RedirectToLocal(returnUrl); } return RedirectToAction(nameof(HomeController.Index), "Home"); } catch (WrongUserNameOrPasswordException) { ModelState.AddModelError("", "用户名或者密码错误"); return View(model); } catch (UserAccountRejectedException) { ModelState.AddModelError("", "用户未通过审核"); return View(model); } catch (AgentAccountRejectedException) { ModelState.AddModelError("", "代理商未通过审核"); return View(model); } catch (AgentContractExpiredException) { ModelState.AddModelError("", "代理商合同已到期"); return View(model); } } private ActionResult RedirectToLocal(string returnUrl) { if (Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } return RedirectToAction(nameof(HomeController.Index), "Home"); } [HttpPost] public ActionResult LogOff() { int authenticatedUserId = User.Identity.GetUserId(); _accountService.LogOff(authenticatedUserId); return RedirectToAction(nameof(AccountController.Login), "Account"); } [HttpPost] [AllowAnonymous] public void LogOffAjax() { if (User.Identity.IsAuthenticated) { int authenticatedUserId = User.Identity.GetUserId(); _accountService.LogOff(authenticatedUserId); } } [HttpGet] public ActionResult ChangePassword() { return View(new ChangePasswordInputModel()); } [HttpGet] public ActionResult ChangeUserProfile() { int authenticatedUserId = User.Identity.GetUserId(); UpdateUserInputModel userProfile = _accountService.GetUserForUpdate(authenticatedUserId); return View(userProfile); } [HttpPost] public ActionResult ChangeUserProfile(UpdateUserInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); _accountService.ChangeUserProfile(model); return RedirectToAction(nameof(HomeController.Index), "Home"); } [HttpPost] [ValidateAntiForgeryToken] public async Task ChangePasswordAsync(ChangePasswordInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); try { string userName = User.Identity.Name; await _accountService.ChangePasswordAsync(authenticatedUserId, model); _accountService.LogOff(authenticatedUserId); await _accountService.LoginAsync(new LoginInputModel { UserName = userName, Password = model.NewPassword, RememberMe = true }); return RedirectToAction(nameof(Login)); } #pragma warning disable FHTRoslynChecker_ExceptionCheck // Do not catch Exceptions which are not defined by FHT catch (Exception e) #pragma warning restore FHTRoslynChecker_ExceptionCheck // Do not catch Exceptions which are not defined by FHT { ModelState.AddModelError("", e.Message); return View("~/Views/Account/ChangePassword.cshtml", new ChangePasswordInputModel()); } } [HttpGet] public ActionResult ModifyPasswordSuccess() { return View(); } [HttpPost] public async Task ResetPwdAsync(int userId) { string password = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 6); await _accountService.ResetPassWordAsync(userId, password); return JavaScript(string.Format("alert('此账号密码已重置为:{0}');", password)); } } }