| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- using System;
- using System.Globalization;
- using System.Linq;
- using System.Security.Claims;
- using System.Threading.Tasks;
- using System.Web;
- using System.Web.Mvc;
- using Microsoft.AspNet.Identity;
- using Microsoft.AspNet.Identity.Owin;
- using Microsoft.Owin.Security;
- using Winsoft.GOV.XF.WX.Models;
- namespace Winsoft.GOV.XF.WX.Controllers
- {
- [Authorize]
- public class AccountController : Controller
- {
- private ApplicationSignInManager _signInManager;
- private ApplicationUserManager _userManager;
- public AccountController()
- {
- }
- public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
- {
- UserManager = userManager;
- SignInManager = signInManager;
- }
- public ApplicationSignInManager SignInManager
- {
- get
- {
- return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
- }
- private set
- {
- _signInManager = value;
- }
- }
- public ApplicationUserManager UserManager
- {
- get
- {
- return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
- }
- private set
- {
- _userManager = value;
- }
- }
- // Authorize 操作是当你访问任何
- // 受保护的 Web API 时调用的终结点。如果用户未登录,则将被重定向到
- // Login 页。在成功登录后,你可以调用 Web API。
- [HttpGet]
- public ActionResult Authorize()
- {
- var claims = new ClaimsPrincipal(User).Claims.ToArray();
- var identity = new ClaimsIdentity(claims, "Bearer");
- AuthenticationManager.SignIn(identity);
- return new EmptyResult();
- }
- //
- // GET: /Account/Login
- [AllowAnonymous]
- public ActionResult Login(string returnUrl)
- {
- ViewBag.ReturnUrl = returnUrl;
- return View();
- }
- //
- // POST: /Account/Login
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
- {
- if (!ModelState.IsValid)
- {
- return View(model);
- }
- // 这不会计入到为执行帐户锁定而统计的登录失败次数中
- // 若要在多次输入错误密码的情况下触发帐户锁定,请更改为 shouldLockout: true
- var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
- switch (result)
- {
- case SignInStatus.Success:
- return RedirectToLocal(returnUrl);
- case SignInStatus.LockedOut:
- return View("Lockout");
- case SignInStatus.RequiresVerification:
- return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
- case SignInStatus.Failure:
- default:
- ModelState.AddModelError("", "无效的登录尝试。");
- return View(model);
- }
- }
- //
- // GET: /Account/VerifyCode
- [AllowAnonymous]
- public async Task<ActionResult> VerifyCode(string provider, string returnUrl, bool rememberMe)
- {
- // 要求用户已通过使用用户名/密码或外部登录名登录
- if (!await SignInManager.HasBeenVerifiedAsync())
- {
- return View("Error");
- }
- return View(new VerifyCodeViewModel { Provider = provider, ReturnUrl = returnUrl, RememberMe = rememberMe });
- }
- //
- // POST: /Account/VerifyCode
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> VerifyCode(VerifyCodeViewModel model)
- {
- if (!ModelState.IsValid)
- {
- return View(model);
- }
- // 以下代码可以防范双重身份验证代码遭到暴力破解攻击。
- // 如果用户输入错误代码的次数达到指定的次数,则会将
- // 该用户帐户锁定指定的时间。
- // 可以在 IdentityConfig 中配置帐户锁定设置
- var result = await SignInManager.TwoFactorSignInAsync(model.Provider, model.Code, isPersistent: model.RememberMe, rememberBrowser: model.RememberBrowser);
- switch (result)
- {
- case SignInStatus.Success:
- return RedirectToLocal(model.ReturnUrl);
- case SignInStatus.LockedOut:
- return View("Lockout");
- case SignInStatus.Failure:
- default:
- ModelState.AddModelError("", "代码无效。");
- return View(model);
- }
- }
- //
- // GET: /Account/Register
- [AllowAnonymous]
- public ActionResult Register()
- {
- return View();
- }
- //
- // POST: /Account/Register
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> Register(RegisterViewModel model)
- {
- if (ModelState.IsValid)
- {
- var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Hometown = model.Hometown };
- var result = await UserManager.CreateAsync(user, model.Password);
- if (result.Succeeded)
- {
- await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
- // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
- // 发送包含此链接的电子邮件
- // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
- // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
- // await UserManager.SendEmailAsync(user.Id, "确认你的帐户", "请通过单击<a href=\"" + callbackUrl + "\">此处</a>来确认你的帐户");
- return RedirectToAction("Index", "Home");
- }
- AddErrors(result);
- }
- // 如果我们进行到这一步时某个地方出错,则重新显示表单
- return View(model);
- }
- //
- // GET: /Account/ConfirmEmail
- [AllowAnonymous]
- public async Task<ActionResult> ConfirmEmail(string userId, string code)
- {
- if (userId == null || code == null)
- {
- return View("Error");
- }
- var result = await UserManager.ConfirmEmailAsync(userId, code);
- return View(result.Succeeded ? "ConfirmEmail" : "Error");
- }
- //
- // GET: /Account/ForgotPassword
- [AllowAnonymous]
- public ActionResult ForgotPassword()
- {
- return View();
- }
- //
- // POST: /Account/ForgotPassword
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
- {
- if (ModelState.IsValid)
- {
- var user = await UserManager.FindByNameAsync(model.Email);
- if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
- {
- // 请不要显示该用户不存在或者未经确认
- return View("ForgotPasswordConfirmation");
- }
- // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
- // 发送包含此链接的电子邮件
- // string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
- // var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
- // await UserManager.SendEmailAsync(user.Id, "重置密码", "请通过单击<a href=\"" + callbackUrl + "\">此处</a>来重置你的密码");
- // return RedirectToAction("ForgotPasswordConfirmation", "Account");
- }
- // 如果我们进行到这一步时某个地方出错,则重新显示表单
- return View(model);
- }
- //
- // GET: /Account/ForgotPasswordConfirmation
- [AllowAnonymous]
- public ActionResult ForgotPasswordConfirmation()
- {
- return View();
- }
- //
- // GET: /Account/ResetPassword
- [AllowAnonymous]
- public ActionResult ResetPassword(string code)
- {
- return code == null ? View("Error") : View();
- }
- //
- // POST: /Account/ResetPassword
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model)
- {
- if (!ModelState.IsValid)
- {
- return View(model);
- }
- var user = await UserManager.FindByNameAsync(model.Email);
- if (user == null)
- {
- // 请不要显示该用户不存在
- return RedirectToAction("ResetPasswordConfirmation", "Account");
- }
- var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
- if (result.Succeeded)
- {
- return RedirectToAction("ResetPasswordConfirmation", "Account");
- }
- AddErrors(result);
- return View();
- }
- //
- // GET: /Account/ResetPasswordConfirmation
- [AllowAnonymous]
- public ActionResult ResetPasswordConfirmation()
- {
- return View();
- }
- //
- // POST: /Account/ExternalLogin
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public ActionResult ExternalLogin(string provider, string returnUrl)
- {
- // 请求重定向到外部登录提供程序
- return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
- }
- //
- // GET: /Account/SendCode
- [AllowAnonymous]
- public async Task<ActionResult> SendCode(string returnUrl, bool rememberMe)
- {
- var userId = await SignInManager.GetVerifiedUserIdAsync();
- if (userId == null)
- {
- return View("Error");
- }
- var userFactors = await UserManager.GetValidTwoFactorProvidersAsync(userId);
- var factorOptions = userFactors.Select(purpose => new SelectListItem { Text = purpose, Value = purpose }).ToList();
- return View(new SendCodeViewModel { Providers = factorOptions, ReturnUrl = returnUrl, RememberMe = rememberMe });
- }
- //
- // POST: /Account/SendCode
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> SendCode(SendCodeViewModel model)
- {
- if (!ModelState.IsValid)
- {
- return View();
- }
- // 生成令牌并发送该令牌
- if (!await SignInManager.SendTwoFactorCodeAsync(model.SelectedProvider))
- {
- return View("Error");
- }
- return RedirectToAction("VerifyCode", new { Provider = model.SelectedProvider, ReturnUrl = model.ReturnUrl, RememberMe = model.RememberMe });
- }
- //
- // GET: /Account/ExternalLoginCallback
- [AllowAnonymous]
- public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
- {
- var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
- if (loginInfo == null)
- {
- return RedirectToAction("Login");
- }
- // 如果用户已具有登录名,则使用此外部登录提供程序将该用户登录
- var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
- switch (result)
- {
- case SignInStatus.Success:
- return RedirectToLocal(returnUrl);
- case SignInStatus.LockedOut:
- return View("Lockout");
- case SignInStatus.RequiresVerification:
- return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
- case SignInStatus.Failure:
- default:
- // 如果用户没有帐户,则提示该用户创建帐户
- ViewBag.ReturnUrl = returnUrl;
- ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
- return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
- }
- }
- //
- // POST: /Account/ExternalLoginConfirmation
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
- {
- if (User.Identity.IsAuthenticated)
- {
- return RedirectToAction("Index", "Manage");
- }
- if (ModelState.IsValid)
- {
- // 从外部登录提供程序中获取有关用户的信息
- var info = await AuthenticationManager.GetExternalLoginInfoAsync();
- if (info == null)
- {
- return View("ExternalLoginFailure");
- }
- var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Hometown = model.Hometown };
- var result = await UserManager.CreateAsync(user);
- if (result.Succeeded)
- {
- result = await UserManager.AddLoginAsync(user.Id, info.Login);
- if (result.Succeeded)
- {
- await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
- return RedirectToLocal(returnUrl);
- }
- }
- AddErrors(result);
- }
- ViewBag.ReturnUrl = returnUrl;
- return View(model);
- }
- //
- // POST: /Account/LogOff
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult LogOff()
- {
- AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
- return RedirectToAction("Index", "Home");
- }
- //
- // GET: /Account/ExternalLoginFailure
- [AllowAnonymous]
- public ActionResult ExternalLoginFailure()
- {
- return View();
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (_userManager != null)
- {
- _userManager.Dispose();
- _userManager = null;
- }
- if (_signInManager != null)
- {
- _signInManager.Dispose();
- _signInManager = null;
- }
- }
- base.Dispose(disposing);
- }
- #region 帮助程序
- // 用于在添加外部登录名时提供 XSRF 保护
- private const string XsrfKey = "XsrfId";
- private IAuthenticationManager AuthenticationManager
- {
- get
- {
- return HttpContext.GetOwinContext().Authentication;
- }
- }
- private void AddErrors(IdentityResult result)
- {
- foreach (var error in result.Errors)
- {
- ModelState.AddModelError("", error);
- }
- }
- private ActionResult RedirectToLocal(string returnUrl)
- {
- if (Url.IsLocalUrl(returnUrl))
- {
- return Redirect(returnUrl);
- }
- return RedirectToAction("Index", "Home");
- }
- internal class ChallengeResult : HttpUnauthorizedResult
- {
- public ChallengeResult(string provider, string redirectUri)
- : this(provider, redirectUri, null)
- {
- }
- public ChallengeResult(string provider, string redirectUri, string userId)
- {
- LoginProvider = provider;
- RedirectUri = redirectUri;
- UserId = userId;
- }
- public string LoginProvider { get; set; }
- public string RedirectUri { get; set; }
- public string UserId { get; set; }
- public override void ExecuteResult(ControllerContext context)
- {
- var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
- if (UserId != null)
- {
- properties.Dictionary[XsrfKey] = UserId;
- }
- context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
- }
- }
- #endregion
- }
- }
|