Asp.net Global.asax问题 [英] Asp.net Global.asax question

查看:73
本文介绍了Asp.net Global.asax问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

wed config

wed config

<location path="~/elmah.axd">
    <system.web>
      <authorization>
        <allow roles="Administrator"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
</configuration>





和我的appl_benRequest



and my appl_benRequest

protected void Application_BeginRequest()
   {
       if (Request.Url.AbsolutePath.ToLowerInvariant().Contains("elmah.axd"))
       {
           // Check if user can see elmah and handle unauthorised users (return 401/redirect to login page/etc...)


       }
how to find login person roleid here plz help me out ...Thanks a lot in advance

   }

推荐答案

有很多方法可能对您有所帮助。



查看System.Web.Security命名空间



//查看当前用户是否处于管理员角色

User.IsInRole(管理员)



//给出当前用户所有角色的列表

Roles.GetRolesForUser ( User.Identity.Name)



//给出管理员角色中所有用户的列表

Roles.GetUsersInRole(Administrator)



如果我需要检查用户是否处于管理员角色,我会这样做

if(User.IsInRole(Administrator) )

{

//代码

}
There are a number of methods that may be helpful to you.

Check out the System.Web.Security namespace

//find out if the current user is in the administrators role
User.IsInRole("Administrator")

//gives a list of all roles the current user is in
Roles.GetRolesForUser(User.Identity.Name)

//gives list of all users in administrators role
Roles.GetUsersInRole("Administrator")

If i needed to check if a user was in the Administrator role, i would do this
if (User.IsInRole("Administrator"))
{
//code
}


对不起,我无法弄清楚如何添加评论中的代码,所以我只是提交另一个答案。



Sorry, i cannot figure out how to add code in a comment, so i am just submitting another answer.

A couple of things I noticed.

You are doing this in your web.cofig:
<pre>
<location path="~/elmah.axd">
    <system.web>
      <authorization>
        <allow roles="Administrator"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
</pre>
Now, if that is setup correctly, when someone not in the administrator group tries to view the elmah page, it will give them an access denied message.  There is no reason to check again in the global.asax.
Try changing:
<pre>
<location path="~/elmah.axd">
</pre>
To:
<pre>
<location path="elmah.axd">
</pre>

Even if you MUST check in the global.asax for some reason, the place would not be application_BeginRequest
Here is a link to an article that may help you find the right method to use in the Global.asax

http://stackoverflow.com/questions/3072768/net-application-beginrequest-how-to-get-user-reference


这篇关于Asp.net Global.asax问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆