一、Owin Identity的使用

时间:2023-03-09 19:41:46
一、Owin Identity的使用

参照http://www.cnblogs.com/r01cn/p/5177708.html教程。

注意点:

1、Nuget包分别下载Microsoft.AspNet.Identity(Owin必须),

Microsoft.AspNet.Identity.EntityFramework(UserStore用,如果不用EF的表,可自定用户表等,但需自定义UserStore,那么此处的Microsoft.AspNet.Identity.EntityFramework可以不引用,此处不做详细解释)

1、VS2013创建的Microsoft.AspNet.Identity.Owin默认为1.0.0版本,需更新到2.2.1,1.0.0缺少不少特性,这样才能编译通过。

2、Microsoft.AspNet.Identity.EntityFramework 1.0.0也要升级,因为1.0.0下的IdentityUser只有7个属性:Claims、Id、Logins、PasswordHash、Roles、SecurityStamp、UserName,少了2个属性:Email、PhoneNumber。

3、顺手再把Microsoft.Owin.Host.SystemWeb -Version 也更新到最新版本2.1.0。

PS:

1、不必直接使用IdentityUser,可以新建一个AppUser继承IdentityUser,这样可以随要求自行添加属性了。

2、在1的基础上注意属性上面的Serialization、Requirs等属性按需求添加,否则会出现:Store does not implement IQueryableUserStore<TUser>。(之前由于Microsoft.AspNet.Identity.EntityFramework版本低(1.0.0)导致AppUser没有Email属性,我自己就加了Email,然后无法编译,是因为缺少头部属性导致的)

3、User中有很多属性是扩展出来的,这些扩展的方法都来自IdentityExtensions,所以关键地方还是要引用 using Microsoft.AspNet.Identity;

待解决:

1、ModelState.IsValid

2、ModelState.AddModelError