.自定义实现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接口的更多相关文章

  1. Asp.net 面向接口可扩展框架之“Mvc扩展框架及DI”

    标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把以前的那个Mvc分区扩展框架迁移过来,并优化整 ...

  2. 面向接口可扩展框架之“Mvc扩展框架及DI”

    面向接口可扩展框架之“Mvc扩展框架及DI” 标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把 ...

  3. MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串

    原文:MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串 如何让视图通过某种途径,把符合日期格式的字符串放到路由中,再传递给类型为DateTime的控制 ...

  4. 【3】.net MVC 使用IPrincipal进行Form登录即权限验证

    1.在MVC项目中添加用户类,可以根据实际项目需求添加必要属性 public class UserData { /// <summary> /// ID /// </summary& ...

  5. Surface Pro 4 和 Surface Book 使用名为 Surface UEFI(统一可扩展固件接口)的新固件接口

    Surface Pro 4 和 Surface Book 使用名为 Surface UEFI(统一可扩展固件接口)的新固件接口.Surface UEFI 提供新功能,如启动更快速.安全性更高.可替换 ...

  6. MVC 扩展 Html.ImageFor

    Asp.Net MVC 扩展 Html.ImageFor 方法详解 背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelpe ...

  7. 前端基于easyui的mvc扩展(续)

    前端基于easyui的mvc扩展(续) 回顾及遗留问题 上一篇讲解了基于easyui的mvc扩展的基本实现,已经降低了在mvc内使用easyui的难度,但是仍然还有一些问题: 当我们要给生成的控件设置 ...

  8. typescript接口扩展、接口的继承

    //接口扩展:接口可以继承接口 // interface Animal{ // eat():void; // } // interface Person extends Animal{ // work ...

  9. ASP.NET MVC扩展库

    很多同学都读过这篇文章吧 ASP.NET MVC中你必须知道的13个扩展点,今天给大家介绍一个ASP.NET MVC的扩展库,主要就是针对这些扩展点进行.这个项目的核心是IOC容器,包括Ninject ...

随机推荐

  1. qsort库函数的用法

    qsort 功 能: 使用快速排序例程进行排序  用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *, ...

  2. 如何禁止 Mac OS X 在外接设备上生成 .DS_Store 文件?以及如何批量删除 .DS_Store 文件?

    如何禁止 Mac OS X 在外接设备上生成 .DS_Store 文件?以及如何批量删除 .DS_Store 文件?原文链接:http://www.java2class.net/bbs/viewthr ...

  3. 深入了解jQuery之整体架构

    本文是在阅读了Aaron艾伦的jQuery源码解析(地址:http://www.imooc.com/learn/172)后的个人体会以及笔记.在这里感谢艾伦老师深入浅出的讲解!! 先来看看如何生成一个 ...

  4. SCN

    SCN 一.SCN的引入 众所周知,当oracle实例崩溃时,oracle通过检查点队列使用CKPT进程,周期性的将LRBA记入控制文件,以记录读取REDO LOG的范围.确定范围之后,oracle首 ...

  5. python3.5------用户的三次验证

    笔者QQ: 360212316 逻辑图 代码 #/usr/bin/python # -*- coding: utf-8 -*- lock = open("black_user_list.tx ...

  6. 使用HTML5新支持的搭建WebRtc环境来作为视频通讯

    发现如果再重新设计这块的话,又会有不同的思路.对于可定位能力,我们可以全息日志采集,将每个用户在整个系统的走向异步的抓取下来,再同步到专门的日志分析系统,在这个系统中可以根据用户号码.订单号进行过滤分 ...

  7. Spring文件上传配置

    增加依赖jar包 <dependency> <groupId>commons-fileupload</groupId> <artifactId>comm ...

  8. 将golang程序注册为windows服务

    1.go get  bitbucket.org/kardianos/service 2.参考里面的exmaple,就可以了 我承认我有点蛋疼

  9. 学习C:程序

    #include <stdio.h> main(){ /*将输入复制到输出,并将其中连续的多个空格用一个空格代替*/ int c,nb; nb = 0; while((c = getcha ...

  10. libcurl教程

    名称 libcurl 的编程教程 目标 本文档介绍使用libcurl编程的一般原则和一些基本方法.本文主要是介绍 c 语言的调用接口,同时也可能很好的适用于其他类 c 语言的接口. 跨平台的可移植代码 ...