[Solution] ASP.NET Identity(2) 空的项目使用
2015-08-17 14:29
876 查看
在本节中,我将说明将ASP.NET Identity添加到现有的项目或者一个空项目。我将介绍你需要添加的Nuget和Class。此示例中,会使用LocalDB。
本节目录:
注册用户
登入登出
注册用户
注册用户涉及到的EF和Identity.Core 2个程序集。
新建项目
新建1个MVC项目或者一个空的WebForm项目都可以,在这里我使用MVC5(with no authentication)。
AccountController
这里需要一个LoginModel
添加View
Login
Index
开始登录
填写登录信息,点击登录
登录成功(点击退出,即可登出用户)
本文作者:Never、C
本文链接:http://www.cnblogs.com/neverc/p/4730439.html
本节目录:
注册用户
登入登出
注册用户
注册用户涉及到的EF和Identity.Core 2个程序集。
新建项目
新建1个MVC项目或者一个空的WebForm项目都可以,在这里我使用MVC5(with no authentication)。
using System.Linq; using System.Web; using EmptyMVC.Models.Account; using System.Web.Mvc; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Owin.Security; namespace EmptyMVC.Controllers { public class AccountController : Controller { public ActionResult Index() { return View(User); } public ActionResult LogOff() { var authenticationManager = HttpContext.GetOwinContext().Authentication; authenticationManager.SignOut(); return Redirect("Login"); } public ActionResult Login() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Login(LoginModel model) { if (ModelState.IsValid) { var userStore = new UserStore<IdentityUser>(); var userManager = new UserManager<IdentityUser>(userStore); var user = userManager.Find(model.Name, model.Pwd); if (user != null) { var authenticationManager = HttpContext.GetOwinContext().Authentication; var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); authenticationManager.SignIn(userIdentity); return LoginRedirect(); } } return View(model); } // // GET: /Account/ public ActionResult Register() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // UserStore 默认构造函数会使用默认连接字符串: DefaultConnection var userStore = new UserStore<IdentityUser>(); var manager = new UserManager<IdentityUser>(userStore); var user = new IdentityUser() { UserName = model.Name }; var result = manager.Create(user, model.Pwd); if (result.Succeeded) { var authenticationManager = HttpContext.GetOwinContext().Authentication; var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity); return LoginRedirect(); } var erro = result.Errors.FirstOrDefault(); ModelState.AddModelError("", erro); } return View(model); } private ActionResult LoginRedirect() { var url = HttpContext.Request["returnurl"]; if (string.IsNullOrEmpty(url)) return Redirect(Url.Action("Index")); return Redirect(url); } } }
AccountController
这里需要一个LoginModel
public class LoginModel { [Required] [Display(Name = "用户名")] public string Name { get; set; } [Required] [StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "密码")] public string Pwd { get; set; } }
添加View
Login
@model EmptyMVC.Models.Account.LoginModel @{ ViewBag.Title = "Login"; } <h2>Login</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>LoginModel</h4> <hr /> @Html.ValidationSummary(true) <div class="form-group"> @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Pwd, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Pwd) @Html.ValidationMessageFor(model => model.Pwd) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="登录" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("回到首页", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
Index
@using Microsoft.AspNet.Identity @model System.Security.Principal.IPrincipal @{ ViewBag.Title = "Index"; } <h2>Index</h2> @if (Model.Identity.IsAuthenticated) { <h3>Hello @Model.Identity.GetUserName() !</h3> using (Html.BeginForm("LogOff","Account")) { Html.AntiForgeryToken(); <input type="submit" value="退出"/> } } else { <ul class="nav navbar-nav navbar-right"> <li>@Html.ActionLink("注册", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li> <li>@Html.ActionLink("登录", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li> </ul> }
开始登录
填写登录信息,点击登录
登录成功(点击退出,即可登出用户)
本文作者:Never、C
本文链接:http://www.cnblogs.com/neverc/p/4730439.html
相关文章推荐
- MetaSploit framework基础知识
- asp.net导出Excel生成多个Sheet
- asp.net 母版页技术
- 笔记三之AsProtect
- [.Net码农]ASP.NET缓存管理的几种方法
- [.Net码农]asp.net下载文件几种方式
- ASP.NET(2)——Web服务器对象
- ASP.NET MVC5 高级教程 学习笔记
- ASP.NET OAuth:解决refresh token无法刷新access token的问题
- Asp.net初识
- ASP.NET第一章总结
- ASP.Net MVC初学
- 在ASP.NET Web API中使用OData
- IE11和传统asp.net的兼容问题
- 无法找到元素 'aop:aspectj-autoproxy' 的声明
- 用Python让Raspberry Pi“动”起来
- Raspberry PI语音控制-PocketSphinx
- Attribute(两)——定义自己的特色+Asp.net MVC中间filter详细解释
- Asp.Net WebForm和MVC同样优秀!
- asp.net 导出Excel