50 c# authentication asp.net-mvc principal
我看过以下两个可访问的布尔值:
System.Web.Mvc.Controller.User.Identity.IsAuthenticated
System.Web.Mvc.Controller.Request.IsAuthenticated
这些之间有区别吗?他们似乎都做同样的事情所以我不确定使用哪个.
我想做的是:
@if (User.Identity.IsAuthenticated) {
if (User.IsInRole("Admin")) {
@Html.ActionLink("Admin", "AdminController")
}
}
Run Code Online (Sandbox Code Playgroud)
要么
@if (Request.IsAuthenticated) {
if (User.IsInRole("Admin")) {
@Html.ActionLink("Admin", "AdminController")
}
}
Run Code Online (Sandbox Code Playgroud)
上述任何一种都可以同样好用吗?
Dar*_*rov 66
没有区别.唯一的区别是,如果用户未经过身份验证,则User.Identity
可能为null,因此您可能获得NRE,而使用第二种方法,内部则检查此更安全.
以下是该Request.IsAuthenticated
方法的实现方式:
public bool IsAuthenticated
{
get
{
return this._context.User != null &&
this._context.User.Identity != null &&
this._context.User.Identity.IsAuthenticated;
}
}
Run Code Online (Sandbox Code Playgroud)
基本上它比第一个更安全.
归档时间: |
|
查看次数: |
45349 次 |
最近记录: |