Asp.Net Identity自定义user类的运用,ClaimsIdentity
mvc5自动生成的用户验证是比较好用的,还可以扩展,可是要求code first,目前使用sqlite,支持entity framework,但不支持code first。
只有自已简单模仿一下了。经过实验,如下几条后,可以运行了。
webconfig
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="owin:AutomaticAppStartup" value="true" />
</appSettings>
Startup.Auth.cs
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
});
}
}
claim identity 相关接口,方法
public interface IUserIdentity
{
string Id { get; set; }
string UserName { get; set; }
} public static ClaimsIdentity CreateIdentity<TUserIdentity>(TUserIdentity user)
where TUserIdentity : IUserIdentity
{
ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
identity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity"));
identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));
//identity.AddClaim(new Claim(ClaimTypes.PrimarySid, user.Id));
//identity.AddClaim(new Claim("DisplayName", user.UserName));
return identity;
}
signinmanager
private IAuthenticationManager AuthenticationManager
{
get { return HttpContext.GetOwinContext().Authentication; }
} var user = AppUserIdentityService.FindAsync(loginViewModel.UserName, loginViewModel.Password, db.MyUsers);
if (user == null || user.Result == null)
ModelState.AddModelError("", "用户名或密码不正确");
else
{
ClaimsIdentity identity = AppUserIdentityService.CreateIdentity(user.Result);
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = loginViewModel.RememberMe }, identity);
return RedirectToLocal(returnUrl);
}
Asp.Net Identity自定义user类的运用,ClaimsIdentity的更多相关文章
- asp.net mvc自定义JsonResult类来防止MaxJsonLength超过限制
前不久在做一个项目的时候,我用到了mvc的webapi返回了一个大数据,结果报了500错误,如下图所示: Server Error in ‘/’ Application. Error during s ...
- Asp.net MVC 自定义异常处理类
using ElegantWM.Common; using System; using System.Collections.Generic; using System.Linq; using Sys ...
- 【ASP.NET Identity系列教程(一)】ASP.NET Identity入门
注:本文是[ASP.NET Identity系列教程]的第一篇.本系列教程详细.完整.深入地介绍了微软的ASP.NET Identity技术,描述了如何运用ASP.NET Identity实现应用程序 ...
- ASP.NET Identity 一 (转载)
来源:http://www.cnblogs.com/r01cn/p/5194257.html 注:本文是[ASP.NET Identity系列教程]的第一篇.本系列教程详细.完整.深入地介绍了微软的A ...
- ASP.NET Identity系列教程-2【Identity入门】
https://www.cnblogs.com/r01cn/p/5177708.html13 Identity入门 Identity is a new API from Microsoft to ma ...
- ASP.NET MVC 随想录——探索ASP.NET Identity 身份验证和基于角色的授权,中级篇
在前一篇文章中,我介绍了ASP.NET Identity 基本API的运用并创建了若干用户账号.那么在本篇文章中,我将继续ASP.NET Identity 之旅,向您展示如何运用ASP.NET Ide ...
- ASP.NET Identity 系列笔记目录
编写目的 混迹博客园已经有一段时间了,一直都是在学习各路大神的文章,自己却没有做出什么贡献,所以觉得应该写一点点内容和大家一起分享.但是本人实在才疏学浅,有心无力啊!正好最近在学习 Microsoft ...
- ASP.NET Identity 身份验证和基于角色的授权
ASP.NET Identity 身份验证和基于角色的授权 阅读目录 探索身份验证与授权 使用ASP.NET Identity 身份验证 使用角色进行授权 初始化数据,Seeding 数据库 小结 在 ...
- ASP.NET Core Identity自定义数据库结构和完全使用Dapper而非EntityFramework Core
前言 原本本节内容是不存在的,出于有几个人问到了我:我想使用ASP.NET Core Identity,但是我又不想使用默认生成的数据库表,想自定义一套,我想要使用ASP.NE Core Identi ...
随机推荐
- gcc基本用法
GCC基本用法 GCC最基本的用法是: gcc [option] filenames option:编译器所需要的编译选项 filenames:要编译的文件名 gcc编译流程 都以 hello.c 为 ...
- CSS基础笔记
之前没有开通好博客,笔记都记录在有道云,今天全部转过来!!! 1.当同一个html元素不止一个样式定义时,内联样式(在html元素内部)拥有最高的优先权:其他如内部样式表(位于<head> ...
- 【转载】ADO.NET与ORM的比较(2):NHibernate实现CRUD
[转载]ADO.NET与ORM的比较(2):NHibernate实现CRUD 转自周公 说明:个人感觉在Java领域大型开发都离不了ORM的身影,所谓的SSH就是Spring+Struts+Hibe ...
- MVC 界面开发
1.什么是设计模式 mvc只是其中一种,对某一类具体问题,总结出来的一套最优的解决方案 1.MVC: 1.Model(模型) View(视图) Controller(控制器) 的缩写 M ...
- C#代码启用事务锁Transaction进行一系列提交回滚操作
一.前言 因为很多人一般进行一系列相关数据库操作都是在存储过程里面,而且在存储过程用锁的写法也是很简单的,在这篇文章主要介绍一下C#后台代码用锁进行一系列事务操作,我建立一个简单的winform程序, ...
- uva 1597 Searching the Web
The word "search engine" may not be strange to you. Generally speaking, a search engine se ...
- O(1)时间删除链表节点
问题描述:给定单相链表的头指针和一个节点指针,定义一个函数在O(1)时间删除该节点. 这个比较简单,做不做解释,直接看参考代码,不过有一点就是要注意,还是要看删除的节点类型,不能保证总是O(1)时间 ...
- yii2 去掉index.php的方法
1.开启apache-rewrite 在Windows下,我们一般使用的是Administrator账号,所以启用这两项非常简单: 在[Apache安装目录]/conf/httpd.conf中找到 # ...
- [C++程序设计]多维数组元素的地址
设有一个二维数组a,它有3行4列.它的定义为int a[3][4]={{1,3,5,7},{9,11,13,15},{17,18,21,23}};a是一个数组名.a数组包含3行,即3个元 素:a[0] ...
- project euler 19: Counting Sundays
import datetime count = 0 for y in range(1901,2001): for m in range(1,13): if datetime.datetime(y,m, ...