认识HttpContext.User
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的更多相关文章
- 异步 HttpContext.Current 为空null 另一种解决方法
1.场景 在导入通讯录过程中,把导入的失败.成功的号码数进行统计,然后保存到session中,客户端通过轮询显示状态. 在实现过程中,使用的async调用方法,出现HttpContext.Curren ...
- 解决Asp.net Mvc中使用异步的时候HttpContext.Current为null的方法
在项目中使用异步(async await)的时候发现一个现象,HttpContext.Current为null,导致一系列的问题. 上网查了一些资料后找到了一个对象: System.Threading ...
- HttpContext.Cache属性
HttpContext基于HttpApplication的处理管道,由于HttpContext对象贯穿整个处理过程,所以,可以从HttpApplication处理管道的前端将状态数据传递到管道的后端, ...
- System.Web.HttpContext.Current.Session为NULL解决方法
http://www.cnblogs.com/tianguook/archive/2010/09/27/1836988.html 自定义 HTTP 处理程序,从IHttpHandler继承,在写Sys ...
- Session 、Application 和 HttpContext 的使用区别
在ASP.NET WEB页面开发中,经常会需要保存一些信息,以便在不同的页面或时间段都能够访问到.这其中便会用到Session和Application. Session .Application 和 ...
- Why is HttpContext.Current null after await?
今天在对项目代码进行异步化改进的时候,遇到一个奇怪的问题(莫笑,以前没遇过),正如标题一样,HttpContext.Current 在 await 异步执行之后,就会变为 null. 演示代码: pu ...
- 在ASP.NET Core中怎么使用HttpContext.Current
一.前言 我们都知道,ASP.NET Core作为最新的框架,在MVC5和ASP.NET WebForm的基础上做了大量的重构.如果我们想使用以前版本中的HttpContext.Current的话,目 ...
- HttpContext.Current.Session.SessionID相关问题及备忘
今天Tony提到说我们系统中会利用如下代码来判断用户是否过期. if (string.IsNullOrEmpty(UserContext.ConnectionSessionId)) { LogUIFa ...
- 【C#】关于HttpContext.Current.Request.QueryString 你要知道点
HttpContext.Current.Request.QueryString[ ]括号中是获取另一个页面传过的的参数值 HttpContext.Current.Request.Form[“ID”]· ...
- Asp.Net HttpContext.RemapHandler 用法
最近在看HttpHandler映射过程文章时发现Context对象中有一个RemapHandler方法,它能将当前请求映射到指定的HttpHandler处理,可跳过系统默认的Httphandler.它 ...
随机推荐
- 一个detect问题引发的一系列思考
在用BoneCP的时候,发现一个JVM日志中报了一个异常,大意是“探测(detect)到有数据库链接没有关闭”(不得不说JVM的强大),但是我用的是连接池里面的链接啊,怎么会需要关闭呢? 有问题首先找 ...
- (转)android项目用到的公共类方法
/*** 直接下载图片并加载至控件(非异步加载)* * @param activity* @param urlpath* 图片下载路径* @param imageView* ...
- Backit轻松为您的网站创建备份
随着日益复杂,备份数据,已成为一个经验法则,为维护数据库和重要的企业信息化的大公司和企业.在一个不幸的灾难的情况下,很多企业无法恢复他们的数据,这进一步增加了在他们对所有盈利和亏损的同时,作为他们的电 ...
- vc访问ACCESS数据库
在现代软件开发中,数据库技术被越来越广泛应用,很多项目都存在着大量的数据需要存储,通常都会采用数据库来存储这些数据.最初,数据库厂商推出一个新的数据库产品时,相应的,他会为程序员提供一套访问该数据库的 ...
- 如何配置数据库ODBC数据源
在<调整计算机的设置>中,点击<系统和安全>. 点击<管理工具>. 点击<数据源(ODBC)>. 点击<系统用户>,然后,点击按 ...
- 借助curl理解$GLOBALS['HTTP_RAW_POST_DATA'] ,$_POST, php://input
发送请求代码 post.php <?php $url='http://localhost/web/curl/url.php'; $data='a=123|b=2&c=3'; $heade ...
- module_param 用于动态开启/关闭 驱动打印信息
1.定义模块参数的方法: module_param(name, type, perm); 其中,name:表示参数的名字; type:表示参数的类型; perm:表示参数的访问权限 ...
- Populating Next Right Pointers in Each Node II ?
void connect(TreeLinkNode *root) { if(root==NULL) return; if(root->left&&root->right) ...
- springboot成神之——spring的文件上传
本文介绍spring的文件上传 目录结构 配置application DemoApplication WebConfig TestController 前端上传 本文介绍spring的文件上传 目录结 ...
- 关于XSS漏洞的简介以及分类
不得不说注入的时代已经过去了,最近xss貌似比较热门.我就去恶补了一下,我表示我只是菜鸟,对xss不了解.所以从最基本的学起. 什么xss漏洞? 一.XSS攻击简介 作为一种HTML注入攻击,XSS攻 ...