认识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.它 ...
随机推荐
- 使用微软的MSBuild.exe编译VS .sln .csproj 文件
最近在看一些算法和测试一些程序,以及帮团队测试程序,团队使用了vs开发环境创建的sln项目文件,我使用的是公司的机器,没有任何权限安装程序等操作,但是又需要编译一些程序,所以我想到了,使用MSBuil ...
- Visual Studio Code教程:基础使用和自定义设置
一.界面介绍 1.1 界面介绍 1.2 文件夹和文件的打开 文件——>打开文件夹/打开文件 1.3 新建文件/文件夹 新建文件: a. 文件——>新建文件: b. 按Ctrl+n; c. ...
- 12C 对表分区维护的增强
Oracle Database 12c对表分区变化比较多,共分为下面几点 1.在线移动分区:通过MOVE ONLINE关键字实现在线分区移动.移动过程中,对表和被移动的分区可以执行查询操作, DML语 ...
- npm包的发布
假设该待发布包在你本地的项目为 project1 包的本地安装测试 在发布之前往往希望在本地进行安装测试.那么需要一个其他的项目来本地安装待发布项目. 假设该其他项目为project2.假设proje ...
- java排序。。简单的冒泡排序
总结:一种简单的交换顺序,从数左边开始扫描待排序的元素,在扫描过程中依次对相邻元素进行比较,将较大值后移,每经过一轮排序后,值最大的元素将移到末尾, 此时记下该元素的位置,下一轮排序只需比较到此位置即 ...
- Netty实现简单HTTP服务器
netty package com.dxz.nettydemo.http; import java.io.UnsupportedEncodingException; import io.netty.b ...
- Android BindService中遇到的一个小问题
今天在使用BindService的时候遇到个小问题,我希望通过Bindservice获取到这个服务,然后执行服务内的某个自定义方法,如下: if(bindService==null){ Intent ...
- AJAX流程 代码
var xml = window.XMLHttpRequest ?new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHT ...
- PHP函数(一)-变量
1.全局变量 <?php $a = 1; $b = 2; function test(){ echo $a + $b."<br>"; //运行结果为0 } tes ...
- 使用Selenium对付一个点击游戏
继续来熟悉Selenium的使用,这次来玩一个Html5游戏.原网址在这:http://tianmaying.com/app/clicking/# 游戏是这样的,5秒内你能点击这个按钮几次.一般人都只 ...