HttpContext.User,即IPrincipal

.net源代码

namespace System.Security.Principal
{
/// <summary>Defines the basic functionality of a principal object.</summary>
[__DynamicallyInvokable, ComVisible(true)]
public interface IPrincipal
{
/// <summary>Gets the identity of the current principal.</summary>
/// <returns>The <see cref="T:System.Security.Principal.IIdentity" /> object associated with the current principal.</returns>
[__DynamicallyInvokable]
IIdentity Identity
{
[__DynamicallyInvokable]
get;
}
/// <summary>Determines whether the current principal belongs to the specified role.</summary>
/// <returns>true if the current principal is a member of the specified role; otherwise, false.</returns>
/// <param name="role">The name of the role for which to check membership. </param>
[__DynamicallyInvokable]
bool IsInRole(string role);
}
}
IPrincipal.Identity属性(只读)
.net源代码
/// <summary>Defines the basic functionality of an identity object.</summary>
[__DynamicallyInvokable, ComVisible(true)]
public interface IIdentity
{
/// <summary>Gets the name of the current user.</summary>
/// <returns>The name of the user on whose behalf the code is running.</returns>
[__DynamicallyInvokable]
string Name
{
[__DynamicallyInvokable]
get;
}
/// <summary>Gets the type of authentication used.</summary>
/// <returns>The type of authentication used to identify the user.</returns>
[__DynamicallyInvokable]
string AuthenticationType
{
[__DynamicallyInvokable]
get;
}
/// <summary>Gets a value that indicates whether the user has been authenticated.</summary>
/// <returns>true if the user was authenticated; otherwise, false.</returns>
[__DynamicallyInvokable]
bool IsAuthenticated
{
[__DynamicallyInvokable]
get;
}
}

Identity的种类

 MVC的授权过滤器 AuthorizeAttribute,即利用了Httpcontext.User来验证当前请求是否已被认证。
.net源代码如下
 public class AuthorizeAttribute : FilterAttribute, IAuthorizationFilter
{
protected virtual bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
IPrincipal user = httpContext.User;
return user.Identity.IsAuthenticated && (this._usersSplit.Length <= || this._usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) && (this._rolesSplit.Length <= || this._rolesSplit.Any(new Func<string, bool>(user.IsInRole)));
}
}
 

认识HttpContext.User的更多相关文章

  1. 异步 HttpContext.Current 为空null 另一种解决方法

    1.场景 在导入通讯录过程中,把导入的失败.成功的号码数进行统计,然后保存到session中,客户端通过轮询显示状态. 在实现过程中,使用的async调用方法,出现HttpContext.Curren ...

  2. 解决Asp.net Mvc中使用异步的时候HttpContext.Current为null的方法

    在项目中使用异步(async await)的时候发现一个现象,HttpContext.Current为null,导致一系列的问题. 上网查了一些资料后找到了一个对象: System.Threading ...

  3. HttpContext.Cache属性

    HttpContext基于HttpApplication的处理管道,由于HttpContext对象贯穿整个处理过程,所以,可以从HttpApplication处理管道的前端将状态数据传递到管道的后端, ...

  4. System.Web.HttpContext.Current.Session为NULL解决方法

    http://www.cnblogs.com/tianguook/archive/2010/09/27/1836988.html 自定义 HTTP 处理程序,从IHttpHandler继承,在写Sys ...

  5. Session 、Application 和 HttpContext 的使用区别

    在ASP.NET WEB页面开发中,经常会需要保存一些信息,以便在不同的页面或时间段都能够访问到.这其中便会用到Session和Application. Session .Application 和 ...

  6. Why is HttpContext.Current null after await?

    今天在对项目代码进行异步化改进的时候,遇到一个奇怪的问题(莫笑,以前没遇过),正如标题一样,HttpContext.Current 在 await 异步执行之后,就会变为 null. 演示代码: pu ...

  7. 在ASP.NET Core中怎么使用HttpContext.Current

    一.前言 我们都知道,ASP.NET Core作为最新的框架,在MVC5和ASP.NET WebForm的基础上做了大量的重构.如果我们想使用以前版本中的HttpContext.Current的话,目 ...

  8. HttpContext.Current.Session.SessionID相关问题及备忘

    今天Tony提到说我们系统中会利用如下代码来判断用户是否过期. if (string.IsNullOrEmpty(UserContext.ConnectionSessionId)) { LogUIFa ...

  9. 【C#】关于HttpContext.Current.Request.QueryString 你要知道点

    HttpContext.Current.Request.QueryString[ ]括号中是获取另一个页面传过的的参数值 HttpContext.Current.Request.Form[“ID”]· ...

  10. Asp.Net HttpContext.RemapHandler 用法

    最近在看HttpHandler映射过程文章时发现Context对象中有一个RemapHandler方法,它能将当前请求映射到指定的HttpHandler处理,可跳过系统默认的Httphandler.它 ...

随机推荐

  1. hadoop之 Zookeeper 分布式应用程序协调服务

    (1) Zookeeper 在 Hadoop 集群中的作用 Zookeeper 是分布式管理协作框架,Zookeeper 集群用来保证 Hadoop 集群的高可用,(高可用的含义是:集群中就算有一部分 ...

  2. lvs之 lvs+nginx+tomcat_1、tomcat_2+redis(lvs dr 模式)

    前提:已经安装好 lvs+nginx+tomcat_1.tomcat_2+redis环境 ,可参考 (略有改动,比如tomcat_1.tomcat_2安装在两台机器上,而不是单机多实例 ,自行稍稍变动 ...

  3. flask 之 mongodb

    查看mongod 是否启动,启动了会显示进程ID和程序名 pgrep mongod -l 查找mongod的位置whereis mongod 或locate mongod 启动mongodmongod ...

  4. composer 详解

    composer 详解 http://blog.csdn.net/panpan639944806/article/details/16808261 https://www.phpcomposer.co ...

  5. 在rebar发布的项目中添加监视工具

    默认使用rebar创建的项目没法使用observer,可以如下操作 修改app.src {application, tcp_server, [ {description, ""}, ...

  6. linux(6)

    第十五单元 软件包的管理 [本节内容]1. 使用RPM安装及移除软件(详见linux系统管理P374)1) 掌握RPM的定义:RPM就是Red Hat Package Manger(红帽软件包管理工具 ...

  7. 分布式缓存系统 Memcached 状态机之网络数据读取与解析

    整个状态机的基本流程如下图所示,后续分析将按该流程来进行. 接上节分解,主线程将接收的连接socket分发给了某工作线程,然后工作线程从任务队列中取出该连接socket的CQ_ITEM,开始处理该连接 ...

  8. MongoDB 4.X搭建

    一.MongoDB4.X搭建 1.下载mongdb安装包,在官网上找到对应的版本,我的是centos7 找到上面的连接,通过命令行: 2.将下载的mongodb-linux-x86_64-4.0.0. ...

  9. JAVA 1.7并发之LinkedTransferQueue原理理解

    昨天刚看完BlockingQueue觉得好高级啊,今天扫到1.7就发现了升级版.... 如果对内容觉得不够充分,可以去看http://www.cs.rochester.edu/u/scott/pape ...

  10. 南京邮电大学网络攻防平台WEB题

    平台地址:http://ctf.nuptsast.com/ 签到题: 右键查看源代码,得到flag md5 collision: 传入的a的md5值要为0e开头的,但不能是QNKCDZO,百度一个0e ...