.自定义实现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. delphi Style TBitmapLink

    New Bitmap Links Editor http://docwiki.appmethod.com/appmethod/1.17/topics/en/What's_New A new edito ...

  2. SQL Server中Rowcount与@@Rowcount的用法

    rowcount的用法: rowcount的作用就是用来限定后面的sql在返回指定的行数之后便停止处理,比如下面的示例, set rowcount 10select * from 表A 这样的查询只会 ...

  3. php 文件锁

    当写为LOCK_EX锁 且 读为LOCK_SH锁时最为妥当,经测试,这时情况如下: 1, 先运行w.php,马上支行r.php 后 程序会先等写入后再读取 2, 先运行r.php,再马上支行w.php ...

  4. 《Pro Git》阅读随想

    之前做版本管理,我使用最多的是SVN,而且也只是在用一些最常用的操作.最近公司里很多项目都开始上Git,借这个机会,我计划好好学习一下Git的操作和原理,以及蕴含在其中的设计思想.同事推荐了一本< ...

  5. [JSP]获取时间

    <%@ page contentType="text/html;charset=gb2312" %> <%@ page import="java.uti ...

  6. APS.net controller

    在controller 路由配置为: routes.MapRoute( name:"Default", url:"{controller}/{action}/{id}&q ...

  7. Maven安装与配置

    下载: 1.从官网http://maven.apache.org中下载,下载下来的是一个压缩包,解压即可.因为Maven本身也是用Java实现的.2.Maven的目录结构   /bin; maven的 ...

  8. npm install 出现UNABLE_TO_GET_ISSUER_CERT_LOCALLY

    解决方式 As a workaround you can turn ssl checking off in your .npmrc 执行 npm config set strict-ssl false ...

  9. [译]ABP框架使用AngularJs,ASP.NET MVC,Web API和EntityFramework构建N层架构的SPA应用程序

    本文转自:http://www.skcode.cn/archives/281 本文演示ABP框架如何使用AngularJs,ASP.NET MVC,Web API 和EntityFramework构建 ...

  10. 配置FastReport,FastReport报表加载不出来

    插件链接: Demo地址:http://pan.baidu.com/s/1dEXUvsP FastReport.Net软件地址:https://pan.baidu.com/s/1c2kNBVi     ...