.net mvc 扩展IPrincipal接口
.自定义实现IPrincipal接口的类
interface ICustomPrincipal : IPrincipal
{
string Identifier { get; set; }
string IdentityType { get; set; }
}
public class CustomPrincipal : ICustomPrincipal
{
public IIdentity Identity { get; private set; }
public bool IsInRole(string role) { return false; }
public CustomPrincipal()
{
}
public CustomPrincipal(string identifer)
{
this.Identity = new GenericIdentity(identifer);
} public CustomPrincipal(IIdentity identity)
{
this.Identity = identity;
} public string Identifier
{
get;
set;
} public string IdentityType
{
get;
set;
}
}
public class CustomPrincipalSerializeModel
{
public string Identifier { get; set; }
public string IdentityType { get; set; }
}
.登录成功,存储相关登录信息
CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
serializeModel.Identifier = model.Account;
serializeModel.IdentityType = "email"; JavaScriptSerializer serializer = new JavaScriptSerializer(); string userData = serializer.Serialize(serializeModel); FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
,
model.Account,
DateTime.Now,
DateTime.Now.AddMinutes(),
false,
userData); string encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
Response.Cookies.Add(faCookie);
.在Global文件中注册Application_PostAuthenticateRequest事件
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); JavaScriptSerializer serializer = new JavaScriptSerializer(); CustomPrincipalSerializeModel serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(authTicket.UserData); CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
newUser.Identifier = serializeModel.Identifier;
newUser.IdentityType = serializeModel.IdentityType; HttpContext.Current.User = newUser;
} }
.在BaseController中获取登录相关信息
protected virtual new CustomPrincipal User
{
get { return HttpContext.User as CustomPrincipal; }
}
.方法中使用
User.Identifier
.net mvc 扩展IPrincipal接口的更多相关文章
- Asp.net 面向接口可扩展框架之“Mvc扩展框架及DI”
标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把以前的那个Mvc分区扩展框架迁移过来,并优化整 ...
- 面向接口可扩展框架之“Mvc扩展框架及DI”
面向接口可扩展框架之“Mvc扩展框架及DI” 标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把 ...
- MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串
原文:MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串 如何让视图通过某种途径,把符合日期格式的字符串放到路由中,再传递给类型为DateTime的控制 ...
- 【3】.net MVC 使用IPrincipal进行Form登录即权限验证
1.在MVC项目中添加用户类,可以根据实际项目需求添加必要属性 public class UserData { /// <summary> /// ID /// </summary& ...
- Surface Pro 4 和 Surface Book 使用名为 Surface UEFI(统一可扩展固件接口)的新固件接口
Surface Pro 4 和 Surface Book 使用名为 Surface UEFI(统一可扩展固件接口)的新固件接口.Surface UEFI 提供新功能,如启动更快速.安全性更高.可替换 ...
- MVC 扩展 Html.ImageFor
Asp.Net MVC 扩展 Html.ImageFor 方法详解 背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelpe ...
- 前端基于easyui的mvc扩展(续)
前端基于easyui的mvc扩展(续) 回顾及遗留问题 上一篇讲解了基于easyui的mvc扩展的基本实现,已经降低了在mvc内使用easyui的难度,但是仍然还有一些问题: 当我们要给生成的控件设置 ...
- typescript接口扩展、接口的继承
//接口扩展:接口可以继承接口 // interface Animal{ // eat():void; // } // interface Person extends Animal{ // work ...
- ASP.NET MVC扩展库
很多同学都读过这篇文章吧 ASP.NET MVC中你必须知道的13个扩展点,今天给大家介绍一个ASP.NET MVC的扩展库,主要就是针对这些扩展点进行.这个项目的核心是IOC容器,包括Ninject ...
随机推荐
- mysql日志开启和查看
mysql日志开启和查看. 找到mysql根目录下的my.ini文件,打开在下面插入 log-bin=mysql-bin binlog_format=mixed 然后重启mysql. 在dos端登录m ...
- python开发 Day1 python基础
编程语言主要从以下几个角度为进行分类,编译型和解释型.静态语言和动态语言.强类型定义语言和弱类型定义语言,每个分类代表什么意思呢,我们一起来看一下. 编译和解释的区别是什么? 编译器是把源程序的每一条 ...
- eclipse技巧,快捷键
快捷键: ctrl + 1,快速修复 ctrl + d, 快捷删除行 shift + Enter,快速移动光标到下一行 ctrl + F11,运行代码 alt + ↑/↓,快速移动行 ctrl + a ...
- 一、prolog简介
一般来说,人工智能语言应具备如下特点: 1.具有符号处理能力(即非数值处理能力): 2.适合于结构化程序设计,编程容易: 3. 具有递归功能和回溯功能: 4. 具有人机交互能力: 5. 适合于推理: ...
- javascript验证香港身份证
var assert = require('assert'); describe('test', function() { it('coc', function() { var testCard01 ...
- 转载:log4j.properties log4j.xml 路径问题
自动加载配置文件: (1)如果采用log4j输出日志,要对log4j加载配置文件的过程有所了解.log4j启动时,默认会寻找source folder下的log4j.xml配置文件,若没有,会寻找lo ...
- linux随笔2
---恢复内容开始--- 2016,12,20 在linux里面不写这些扩展名,也是可以的,但是写这些扩展名是为了个管理员写的,好区分,但是linux本身不依靠扩展名来区分文件类型 ~~~~~~~~~ ...
- 分享一个js中的bind方法使用
来源:http://www.cnblogs.com/yuzhongwusan/archive/2012/02/13/2348782.html Js代码 复制代码 代码如下: var first_obj ...
- InstallShield Limited Edition for Visual Studio 2013 图文教程
http://www.wuleba.com/?p=23892 原文链接
- 抢凳子日数据sql
--先找到档期select * from hv_item_info a where a.report_begdate > '2016-07-28' and a.report_begdate &l ...