当他的角色没有足够的权限时,将用户从web.config重定向到另一个页面

mak*_*mbi 3 asp.net redirect asp.net-membership web-config

我正在使用ASP.NET,我希望能够将用户从web配置重定向到另一个页面.

我有一些限制,如:

 <location path="Structures.aspx">
    <system.web>
      <authorization>
        <allow roles="Admin"/>
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
Run Code Online (Sandbox Code Playgroud)

如果我将用户重定向到某个页面,那将会非常棒.我看过 这篇文章,但这不是我想要的.

我需要在web.config中执行此操作,而不是在代码后面执行.谢谢!

Bra*_*tie 5

假设您要处理所有"未授权"错误:

<customErrors defaultRedirect="Error.aspx" mode="On">
    <error statusCode="401" redirect="Unauthorized.aspx" />
    <error statusCode="403" redirect="Forbidden.aspx" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

任何401(未经授权的)请求都将转发给Unauthorized.aspx.

或者,您需要在Page_Load活动中执行检查.如果这看起来很乏味,你总是可以为所有应该只管理的页面创建一个基页类,并在那里执行检查.例如

// base class
public class AdminOnlyPage : Page
{
  /*...*/ Page_Load(Object sender, EventArgs e)
  {
    /* check if the user is admin otherwise reject and redirect */
  }
}

// Your "Structures.aspx" page
public class Structures : AdminOnlyPage
{
}
Run Code Online (Sandbox Code Playgroud)