认识HttpContext.User
2016-04-25 21:51
344 查看
HttpContext.User,即IPrincipal
.net源代码
Identity的种类
.net源代码
namespaceSystem.Security.Principal { ///<summary>Definesthebasicfunctionalityofaprincipalobject.</summary> [__DynamicallyInvokable,ComVisible(true)] publicinterfaceIPrincipal { ///<summary>Getstheidentityofthecurrentprincipal.</summary> ///<returns>The<seecref="T:System.Security.Principal.IIdentity"/>objectassociatedwiththecurrentprincipal.</returns> [__DynamicallyInvokable] IIdentityIdentity { [__DynamicallyInvokable] get; } ///<summary>Determineswhetherthecurrentprincipalbelongstothespecifiedrole.</summary> ///<returns>trueifthecurrentprincipalisamemberofthespecifiedrole;otherwise,false.</returns> ///<paramname="role">Thenameoftheroleforwhichtocheckmembership.</param> [__DynamicallyInvokable] boolIsInRole(stringrole); } }
IPrincipal.Identity属性(只读) .net源代码
///<summary>Definesthebasicfunctionalityofanidentityobject.</summary> [__DynamicallyInvokable,ComVisible(true)] publicinterfaceIIdentity { ///<summary>Getsthenameofthecurrentuser.</summary> ///<returns>Thenameoftheuseronwhosebehalfthecodeisrunning.</returns> [__DynamicallyInvokable] stringName { [__DynamicallyInvokable] get; } ///<summary>Getsthetypeofauthenticationused.</summary> ///<returns>Thetypeofauthenticationusedtoidentifytheuser.</returns> [__DynamicallyInvokable] stringAuthenticationType { [__DynamicallyInvokable] get; } ///<summary>Getsavaluethatindicateswhethertheuserhasbeenauthenticated.</summary> ///<returns>trueiftheuserwasauthenticated;otherwise,false.</returns> [__DynamicallyInvokable] boolIsAuthenticated { [__DynamicallyInvokable] get; } }
Identity的种类
MVC的授权过滤器AuthorizeAttribute,即利用了Httpcontext.User来验证当前请求是否已被认证。 .net源代码如下
publicclassAuthorizeAttribute:FilterAttribute,IAuthorizationFilter { protectedvirtualboolAuthorizeCore(HttpContextBasehttpContext) { if(httpContext==null) { thrownewArgumentNullException("httpContext"); } IPrincipaluser=httpContext.User; returnuser.Identity.IsAuthenticated&&(this._usersSplit.Length<=0||this._usersSplit.Contains(user.Identity.Name,StringComparer.OrdinalIgnoreCase))&&(this._rolesSplit.Length<=0||this._rolesSplit.Any(newFunc<string,bool>(user.IsInRole))); } }
相关文章推荐
- IPerf——网络测试工具介绍与源码解析(3)
- android SDK 更新问题完美解决 http://dl-ssl.google.com refused
- TCP与UDP的区别
- 关于TCP协议握手的那些事儿
- HTTP协议基本知识
- XMLHttpRequest cannot load – Origin is not allowed by Access-Control-Allow-Origin.
- 计算机网络的各类的划分
- TCP/IP 卷一 链路层
- HTTP协议中TCP的三次握手,四次挥手总结
- 【弱校胡策】2016.4.25 (bzoj4108、tsinsenA1339、tsinsenA1490)上下界网络流+树状数组+线段树+矩阵乘法+概率与期望
- python里的坑。http://www.pythoner.com/356.html
- TCP协议详解
- 复杂网络数据网站+程序代码网站
- opencv中使用bp神经网络
- HttpClient使用详解
- 值得收藏关于tcp/ip
- 计算机网络自顶向下方法之一一一第二章之二
- [IDF实验室] CTF手札-牛刀小试
- Android http get请求代码片段
- HTTP基础