.自定义实现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. Python:关于字典的相关操作

    >>> people = {"Tom":170, "Jack":175, "Kite":160, "White& ...

  2. WEB框架

     WEB框架本质                                                                        一.WEB请求流程 所有的web应用,都 ...

  3. C# 一些知识点总结(二)_路径类,编码类,文件类...

    Path 类:路径类path.GetFileName("文件路径")//获取完整文件名,包括文件名和文件拓展名Path.GetFileNameWithoutExtension(&q ...

  4. 记录下最近项目中常用到的SQL语句

    1  实现对字符串的Spilt功能. 比如查出“I have a dream!”总共有几个单词,需要以' '分割,然后再求出总数. ALTER function [dbo].[fc_SpiltStri ...

  5. HashMap、HashTable、LinkedHashMap和TreeMap用法和区别

    Java为数据结构中的映射定义了一个接口java.util.Map,它有四个实现类,分别是HashMap.HashTable.LinkedHashMap和TreeMap.本节实例主要介绍这4中实例的用 ...

  6. MVC使用内建的Form辅助器方法创建Select元素

    第一种方法: List<SelectListItem> statusItems = new List<SelectListItem>();            statusI ...

  7. 笔记26-徐 SQLSERVER内存分配和常见内存问题

    1 --64位SQLSERVER   应用在IA64操作系统                         7TB                         2TB               ...

  8. Android笔记之——事件的发生

    Java:package com.example.helloworld;import android.content.Intent;import android.support.v7.app.AppC ...

  9. 用"时:分:秒"的方式显示运行时间

    import datetime,time start = datetime.datetime.now()...dosomething() end = datetime.datetime.now()pr ...

  10. Win10切换中英输入法问题

    用此方法解决后的效果: Win10系统只剩下"美式键盘"和"搜狗拼音"两种输入法,且默认为美式键盘. 按Ctrl+Shift切换到搜狗拼音,输入完成后,再按Ct ...