VS2013中web项目中自动生成的ASP.NET Identity代码思考
vs2013没有再分webform、mvc、api项目,使用vs2013创建一个web项目模板选MVC,身份验证选个人用户账户。项目会生成ASP.NET Identity的一些代码。这些代码主要在AccountController中。
ASP.NET Identity微软宣称的好处就不说了,这是原文
ASP.NET Identity As the membership story in ASP.NET has evolved over the years, the ASP.NET team has learned a lot from feedback from customers. The assumption that users will log in by entering a user name and password that they have registered in your own application is no longer valid. The web has become more social. Users are interacting with each other in real time through social channels such as Facebook, Twitter, and other social web sites. Developers want users to be able to log in with their social identities so that they can have a rich experience on their web sites. A modern membership system must enable redirection-based log-ins to authentication providers such as Facebook, Twitter, and others. As web development evolved, so did the patterns of web development. Unit testing of application code became a core concern for application developers. In 2008 ASP.NET added a new framework based on the Model-View-Controller (MVC) pattern, in part to help developers build unit testable ASP.NET applications. Developers who wanted to unit test their application logic also wanted to be able to do that with the membership system. Considering these changes in web application development, ASP.NET Identity was developed with the following goals:
• One ASP.NET Identity system •ASP.NET Identity can be used with all of the ASP.NET frameworks, such as ASP.NET MVC, Web Forms, Web Pages, Web API, and SignalR.
• ASP.NET Identity can be used when you are building web, phone, store, or hybrid applications. • Ease of plugging in profile data about the user •You have control over the schema of user and profile information. For example, you can easily enable the system to store birth dates entered by users when they register an account in your application.
Persistence control •By default, the ASP.NET Identity system stores all the user information in a database. ASP.NET Identity uses Entity Framework Code First to implement all of its persistence mechanism.
•Since you control the database schema, common tasks such as changing table names or changing the data type of primary keys is simple to do.
•It's easy to plug in different storage mechanisms such as SharePoint, Windows Azure Storage Table Service, NoSQL databases, etc., without having to throw System.NotImplementedExceptions exceptions. • Unit testability • ASP.NET Identity makes the web application more unit testable. You can write unit tests for the parts of your application that use ASP.NET Identity. • Role provider • There is a role provider which lets you restrict access to parts of your application by roles. You can easily create roles such as “Admin” and add users to roles. • Claims Based • ASP.NET Identity supports claims-based authentication, where the user’s identity is represented as a set of claims. Claims allow developers to be a lot more expressive in describing a user’s identity than roles allow. Whereas role membership is just a boolean (member or non-member), a claim can include rich information about the user’s identity and membership. •Social Login Providers • You can easily add social log-ins such as Microsoft Account, Facebook, Twitter, Google, and others to your application, and store the user-specific data in your application. • Windows Azure Active Directory •You can also add log-in functionality using Windows Azure Active Directory, and store the user-specific data in your application. For more information, see Organizational Accounts in Creating ASP.NET Web Projects in Visual Studio 2013 • OWIN Integration • ASP.NET authentication is now based on OWIN middleware that can be used on any OWIN-based host. ASP.NET Identity does not have any dependency on System.Web. It is a fully compliant OWIN framework and can be used in any OWIN hosted application.
•ASP.NET Identity uses OWIN Authentication for log-in/log-out of users in the web site. This means that instead of using FormsAuthentication to generate the cookie, the application uses OWIN CookieAuthentication to do that. • NuGet package • ASP.NET Identity is redistributed as a NuGet package which is installed in the ASP.NET MVC, Web Forms and Web API templates that ship with Visual Studio 2013. You can download this NuGet package from the NuGet gallery.
•Releasing ASP.NET Identity as a NuGet package makes it easier for the ASP.NET team to iterate on new features and bug fixes, and deliver these to developers in an agile manner.
我的感兴趣的主要是几个方面。1、可以扩展用户类的字段;2、使用EF codefirst存储;3、OWIN集成 。4、基于Claims(Claims是什么,到底有什么用我都不知道,只知道是.NET框架里的一个命名空间)。
我看了整个代码觉得它有点类似于三层架构。这里要特别重要的几个类:
- ApplicationDbContext类。就是继承字自ef的DbContext,EF codefirst的必备。
- UserStore类。封装了数据库访问一系列方法,通过DbContext操作数据库。类似三层架构中的数据访问层。
- UserManager类。用户管理的类,封装了用户、角色、Claim等一系列的方法,通过UserStore类与数据库交互。类似三层的业务逻辑层。
- AuthenticationManager类。Owin的一些东西,包含了用户登录、注销、验证等一些方法。
- ApplicationUser。用户模型。继承自IdentityUser,可以自行扩展属性。
这个东西怎么用呢:
一、设置ApplicationDbContext类
先看下代码:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
这个类继承自IdentityDbContext类,ApplicationUser是用户模型。
这里要设置的就是base("DefaultConnection")。数据库的链接字符串。在web.config的connectionStrings节设置。
我们再分析IdentityDbContext类的元数据。
可以看出它继承自DbContext。共再数据库创建两个表,Users根据传输的用户模型来创建。角色表 Roles表根据IdentityRole来创建(这个类只有两个属性ID和name)看以看出这里角色的字段是不能扩展的。
二、扩展用户模型(ApplicationUser)
当然不扩展可以直接用,当预设不满足要求时就可以自行扩展字段。首先看下代码
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
}
是个继承自IdentityUser的空类,需要扩展的字段直接写这就行,如果扩展个年龄(Age)
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public int Age { get; set; }
}
我们看下其继承的IdentityUser类,这里面都是一些预知的属性。IUser是一个接口只有ID和username两个属性。
public class IdentityUser : IUser
{
public IdentityUser();
public IdentityUser(string userName); public virtual ICollection<IdentityUserClaim> Claims { get; }
public virtual string Id { get; set; }
public virtual ICollection<IdentityUserLogin> Logins { get; }
public virtual string PasswordHash { get; set; }
public virtual ICollection<IdentityUserRole> Roles { get; }
public virtual string SecurityStamp { get; set; }
public virtual string UserName { get; set; }
}
三、利用UserManager,AuthenticationManager进行用户相关操作
这一步就可直接在控制器中写代码了。我们先看下AccountController的构造函数
[Authorize]
public class AccountController : Controller
{
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
注意,UserManager,ApplicationUser,UserStore,ApplicationDbContext。
1、看下用户注册代码
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser() { UserName = model.UserName };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);//一个循环向ModelState添加错误消息的方法
}
} // 如果我们进行到这一步时某个地方出错,则重新显示表单
return View(model);
}
我们来看下这段代码。
public async Task<ActionResult> Register(RegisterViewModel model)这句将Register声明为异步action。
var user = new ApplicationUser() { UserName = model.UserName };这句是ApplicationUser这个创建模型类。
var result = await UserManager.CreateAsync(user, model.Password);
这句是一个异步添加用户。利用UserManager这个类的CreateAsync方法创建用户。
类元数据如下图,里面封装了各种用户操作的方法。
await SignInAsync(user, isPersistent: false);这句依然。这里是AccountController的一个函数,代码如下:
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
可以看出利用AuthenticationManager的SignOut和SignIn进行清除cookie和登录。
2、再看下注销
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Index", "Home");
}
使用AuthenticationManager.SignOut方法。这是类似于 WebFor中的FormsAuthentication所使用的FormsAuthentication.SignOut方法。
3、再看登录代码
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
} // 如果我们进行到这一步时某个地方出错,则重新显示表单
return View(model);
}
代码比较类似,用UserManager的FindAsync查找用户,成功又有是用调用SignInAsync方法进行登录(方法内部是用AuthenticationManager的SignIn进行登录)
四、总结
这里用到的是ApplicationDbContext、UserStore、UserManager、AuthenticationManager、ApplicationUser这5个类。其中ApplicationDbContext需要设置连接字符串,ApplicationUser可以扩展用户字段,UserStore、UserManager、AuthenticationManager这三个类都封装好的类直接拿来用就行,关键是要理解方法和属性的含义、清楚其用途。
五、问题及思考
- 依靠[Authorize]应该是用来验证用户是否登录的,直接用来进行权限管理(控制器上写[Authorize(Roles="管理员")])是不是太僵化了?
- Claims这是东西什么东西,从来没用过,是否与权限有关?
VS2013中web项目中自动生成的ASP.NET Identity代码思考的更多相关文章
- 关于IDEA中web项目中web.xml配置文件标红的解决办法
原文链接 https://blog.csdn.net/qq_33451695/article/details/86684127 解决方法前提:web.xml没有实际错误,但依然被web.xml标红 出 ...
- 用Eclipse新建一个web项目没有自动生成web.xml
我们首先打开Eclipse,如下: 我们可以看到在"WEB-INF"文件夹下没有web.xml文件. 这是是什么原因呢,我们来看看,我们首先来新建一个web工程,如下: ...
- Java Web项目中使用Freemarker生成Word文档
Web项目中生成Word文档的操作屡见不鲜.基于Java的解决方式也是非常多的,包含使用Jacob.Apache POI.Java2Word.iText等各种方式,事实上在从Office 2003開始 ...
- C# 动态创建SQL数据库(二) 在.net core web项目中生成二维码 后台Post/Get 请求接口 方式 WebForm 页面ajax 请求后台页面 方法 实现输入框小数多 自动进位展示,编辑时实际值不变 快速掌握Gif动态图实现代码 C#处理和对接HTTP接口请求
C# 动态创建SQL数据库(二) 使用Entity Framework 创建数据库与表 前面文章有说到使用SQL语句动态创建数据库与数据表,这次直接使用Entriy Framwork 的ORM对象关 ...
- 在基于MVC的Web项目中使用Web API和直接连接两种方式混合式接入
在我之前介绍的混合式开发框架中,其界面是基于Winform的实现方式,后台使用Web API.WCF服务以及直接连接数据库的几种方式混合式接入,在Web项目中我们也可以采用这种方式实现混合式的接入方式 ...
- WEB 项目中的全局异常处理
在web 项目中,遇到异常一般有两种处理方式:try.....catch....:throw 通常情况下我们用try.....catch.... 对异常进行捕捉处理,可是在实际项目中随时的进行异常捕捉 ...
- php课程 1-3 web项目中php、html、js代码的执行顺序是怎样的(详解)
php课程 1-3 web项目中php.html.js代码的执行顺序是怎样的(详解) 一.总结 一句话总结:b/s结构 总是先执行服务器端的先.js是客户端脚本 ,是最后执行的.所以肯定是php先执行 ...
- Web 项目中分享到微博、QQ空间等分享功能
Web 项目中分享到微博.QQ空间等分享功能 网上有很多的模板以及代码,但是有很多都不能分享内容,简单的测试了下: 以新浪微博为例,文本框中的内容是title属性,下面的链接是url属性,如果你的链接 ...
- web项目中加入struts2、spring的支持,并整合两者
Web项目中加入struts2 的支持 在lib下加入strut2的jar包 2. 在web.xml中添加配置 <filter> <filter-name>struts2< ...
随机推荐
- bzoj1503郁闷的出(cheng)纳(xu)员
好痛苦,,,WA了不知道多少遍 错的服了,,, 如果某员工的初始工资低于工资下界,他将立刻离开公司 我也不知道是我语文有问题还是题目有毒,反正这个东西好像不应该算在离开公司的总人数的答案里... 让我 ...
- Redis集群最佳实践
今天我们来聊一聊Redis集群.先看看集群的特点,我对它的理解是要需要同时满足高可用性以及可扩展性,即任何时候对外的接口都要是基本可用的并具备一定的灾备能力,同时节点的数量能够根据业务量级的大小动态的 ...
- 用Eclipse搭建ssh框架
问:ssh是哪三大框架,以及他们的作用是什么? 答:分别是struts,spring,hibernate. struts的作用是:是web层,其核心是mvc模式,他可以自动获取参数,自动类型转换,自动 ...
- BJITJobs : 北京IT圈高端职位招聘信息,成功率最高的高端求职渠道
你有实力,但比你差的人都升了,你的师弟都年薪50万了,你还是找不到机会.为什么你离高端机会总是差一步呢?其实你离成功就差一次机会,一个适合你的高端职位的信息! 招聘网站不靠谱:三大网站都是低端职位为主 ...
- HDu--我要拿走你的蜡烛
我要拿走你的蜡烛 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- Spring MVC注解的一些案列
1. spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...
- angular----关于注入HTML
后台接口返回了一个HTML字符串,要绑定到页面,第一时间想到了innerHTML... 一:先说下一般用法 //原生用法 <div id="content"></ ...
- IOS 真机调试
真机调试的步骤: 1.注册成为苹果开发者(99$) 2.登陆苹果开发者主页 https://developer.apple.com/membercenter/index.action 3.点击 Cer ...
- ASP.NET MVC 多语言方案
前言: 好多年没写文章了,工作很忙,天天加班, 每天都相信不用多久,就会升职加薪,当上总经理,出任CEO,迎娶白富美,走上人生巅峰,想想还有点小激动~~~~ 直到后来发生了邮箱事件,我竟然忘了给邮箱密 ...
- CSS调试小技巧 —— 调试DOM元素hover,focus,actived的样式
最近学习html5和一些UI框架,接触css比较多,就来跟大家分享一下css中的一些调试技巧.之前做页面,css都是自己写的,所以要改哪里可以很快的找到,现在使用了UI框架,里面的样式是不可能读完的, ...