2016-03-21 210 views
0

web.config我们可以为位置单独设置授权选项:检查授权

<location path="request.ashx"> 
     <system.web> 
      <authorization> 
       <allow users="*" /> 
      </authorization> 
     </system.web> 
    </location> 

如何从代码这个选项?

+0

是否使用MVC? –

+0

关于这个问题呢这么说:http://stackoverflow.com/questions/10848086/authorize-attribute-in-asp-net-mvc – pix

+0

我希望你正在寻找这个。 http://stackoverflow.com/questions/1240552/accessing-authorization-information-in-web-config – Hari

回答

0

比使用Authorize属性更好,我认为使用if条件会好得多。

If(User.Identity.IsAuthenticated) 
{ 
    If(User.Identity.IsInRole=="admin") 
    { 
     return view("Secret"); 
    } 
    else 
    { 
      return view("NotAllowed") 
    } 
} 
else 
{ 
    return View("NeedAuth"); 
} 

如果用户突然重定向到loginpage在属性,他可能会认为它是在网站上的错误,但你将能够清楚地告诉他需要对用户进行认证这种方式。

0

试试这个:

public void ProcessRequest(HttpContext context) 
    { 
     if (HttpContext.Current.User.Identity.IsAuthenticated == false) 
     { 
      context.Response.Redirect("Your Path"); 
     } 
    }