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. World、Excel利用流下载

    /** * 下载excel * @param request * @param response * @param filePath * @param fileName */public static ...

  2. Oracle GoldenGate理论

    1Oracle GoldenGate处理方法和支持的数据库Oracle GoldenGate在多样和异构的基础IT平台中,可以在事务级别上进行数据交换和数据操作.在保证交易完整性和最小的开销的条件下, ...

  3. bzoj 1499 [NOI2005]瑰丽华尔兹——单调队列优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1499 简单的单调队列优化dp.(然而当时却WA得不行.今天总算填了坑) 注意滚动数组赋初值应 ...

  4. android通过查询电话号码获取联系人信息

    // 取得Intent中的頭像 ivShowImage = (ImageView) findViewById(R.id.call_log_detail_contact_img); //通话电话号码获取 ...

  5. 平均分割list

    private static List<List<Integer>> splitList(List<Integer> lists,int limit){ int s ...

  6. Oracle视图编译错误解决办法

    因为新搭的环境,数据库是从另一个现成的环境导过来的,直接后台用exp和imp命令操作.但是新环境的Oracle数据库有问题,一些视图创建不了,导致用到这些视图的视图和存储过程也编译不了.后来手工重新编 ...

  7. mysql中修改密码的方式

    参考地址:https://www.cnblogs.com/yang82/p/7794712.html mysql中修改用户密码的方式: 最简单的方法就是借助第三方工具Navicat for MySQL ...

  8. java代码关于匿名内部类和接口的方法使用

    总结:主要是多个按钮实现监听时,能够响应不同的事件 以上步骤我们可以用多种方法实现.但人们通常用二种方法.第一种方法是只利用一个监听器以及多个if语句来决定是哪个组件产生的事件:第二种方法是使用多个内 ...

  9. Java-Maven-Runoob:Maven 自动化构建

    ylbtech-Java-Maven-Runoob:Maven 自动化构建 1.返回顶部 1. 自动化构建定义了这样一种场景: 在一个项目成功构建完成后,其相关的依赖工程即开始构建,这样可以保证其依赖 ...

  10. [置顶] STM32 输入捕获的脉冲宽度及频率计算

    输入捕获模式可以用来测量脉冲宽度或者测量频率.STM32 的定时器,除了 TIM6 和 TIM7,其他定时器都有输入捕获功能.以下是对脉冲宽度及频率的计算. 1.脉冲宽度 如下图所示,采集该高电平脉冲 ...