using Microsoft.AspNet.Identity; using Microsoft.Owin.Security; using OpenAuth.Mvc.Models; using System.Collections.Generic; using System.Security.Claims; using System.Threading.Tasks; using System.Web; using System.Web.Mvc; using Newtonsoft.Json; using OpenAuth.App; using OpenAuth.Domain; using OpenAuth.Domain.Interface; namespace OpenAuth.Mvc.Controllers { [Authorize] public class AccountController : Controller { private IUserRepository _userRepository; public AccountController(IUserRepository repository) { _userRepository = repository; } // // GET: /Account/Login [AllowAnonymous] public ActionResult Login(string returnUrl) { ViewBag.ReturnUrl = returnUrl; return View(); } // // POST: /Account/Login [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { //直接生成登陆用户,在实际的项目中采用数据库形式 var user = new User {Account = "admin"}; if (user != null) { await SignInAsync(user, model.RememberMe); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("", "Invalid username or password."); } } // 如果我们进行到这一步时某个地方出错,则重新显示表单 return View(model); } // // POST: /Account/LogOff public ActionResult LogOff() { AuthenticationManager.SignOut(); return RedirectToAction("Login", "Account"); } public ActionResult List() { return View(); } public string LoadUsers() { return JsonConvert.SerializeObject(_userRepository.LoadUsers()); } #region 帮助程序 private IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } } /// /// sign information as an asynchronous operation. /// /// 用户 /// Remember me? /// Task. private async Task SignInAsync(User user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var claims = new List { new Claim(ClaimTypes.Name, user.Account), new Claim(ClaimTypes.Role, "Administrator"), new Claim(ClaimTypes.NameIdentifier, "7c301fe4-099e-46f9-bdb8-e922d73a8031"), new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity") }; var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); } private ActionResult RedirectToLocal(string returnUrl) { if (Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } #endregion 帮助程序 } }