FormsAuthentication 授权被拒绝消息

发布于 2024-09-10 18:33:44 字数 723 浏览 0 评论 0原文

因此,我已经实现了 IPrincipal.IsInRole(...) 并使用 FormsAuthentication,如下所示:

<authentication mode="Forms">
     <forms loginUrl="Login.aspx" name="someName" timeout="600"/>
</authentication>

然后我有一个页面要求您进行身份验证并且您拥有“roleA”。配置如下:

 <location path="SomePage.aspx">
  <system.web>
   <authorization>
    <allow roles="roleA" />
    <deny users="*"/>
   </authorization>
  </system.web>
 </location>

现在,我登录到我的 Web 应用程序,但使用的用户没有 roleA。当我访问 SomePage.aspx 时,我被重定向到 Login.aspx,即表单元素的 loginUrl 中指定的 url。所以,我的问题是我不应该能够指定授权被拒绝的消息或网址吗?如果用户已通过身份验证,但未授权,为什么我要重定向到登录页面。这让用户感到非常困惑。请告诉我我错过了一些简单的事情。

感谢您的阅读!

So, I've implemented my IPrincipal.IsInRole(...) and I'm using FormsAuthentication like so:

<authentication mode="Forms">
     <forms loginUrl="Login.aspx" name="someName" timeout="600"/>
</authentication>

Then I have a page that requires you to be authenticated and that you have "roleA". This is configured like so:

 <location path="SomePage.aspx">
  <system.web>
   <authorization>
    <allow roles="roleA" />
    <deny users="*"/>
   </authorization>
  </system.web>
 </location>

Now, I login to my web application, but with a user that does NOT have roleA. When I visit SomePage.aspx I get redirected to Login.aspx, the url specified in loginUrl of the forms element. So, my question is shouldn't I be able be specify an authorization denied message or url? If the user is authenticated, but not authorized why would I want to redirect to the login page. It's confusing as hell to the user. Please tell me I am missing something simple.

Thanks for reading!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甚是思念 2024-09-17 18:33:45

Roles.IsUserInRole - 如果您只是将其用于此页面,请将其放入后面的代码中。如果您有很多页面,您可以考虑将其放入基类中并从每个页面的 web.config 或数据库中读取。我相信这会给你最大的控制权。

Roles.IsUserInRole - If you're just using it for this page, throw this in the code behind. If you have a lot of pages, you could consider putting this in a base class and reading either from the web.config or the database per page. I believe this will give you the most control.

我最亲爱的 2024-09-17 18:33:45

是的,这有点烦人。也许有人有更简单的想法,但我们提出的解决方案(hack?)是在用户重定向到登录页面时查找 ASP.NET 附加到查询字符串的最初请求的 URL。

我们创建了一个新的 web.config 部分,用于存储一组将重定向 URL 片段与授权消息相匹配的键/值:

<configSections>
    <section name="authorizationFailureMessages" type="System.Configuration.NameValueSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    ...etc...
</configSections>

<authorizationFailureMessages>
    <add key="MemberResources" value="MembershipRequired" />
    <add key="Staff" value="StaffOnly" />
    <add key="Departments/Administration/BoardOfDirectors" value="BoardOfDirectorsOnly" />
    ...etc...
</authorizationFailureMessages>

在 Login.aspx 页面的 Page_Load() 事件中,我们调用一个使用此 URL 的方法确定发生了哪个(取消)授权事件,然后将其重定向到显示相应文本的消息页面:

private void DisplayAppropriateAuthorizationMessage ()
{
    if ( !Page.User.Identity.IsAuthenticated )
        return;

    string redirectUrl = FormsAuthentication.GetRedirectUrl( Page.User.Identity.Name, false );

    if ( string.IsNullOrEmpty( redirectUrl ) )
        return;

    NameValueCollection authorizationFailureMessages = ConfigurationManager.GetSection( "authorizationFailureMessages" ) as NameValueCollection;

    if ( authorizationFailureMessages == null )
        return;

    foreach ( string key in authorizationFailureMessages.AllKeys )
    {
        if ( redirectUrl.Contains( key ) )
        {
            Response.Redirect( String.Format( "Message.aspx?{0}={1}", Constants.QueryStringKeys.ERRORMESSAGENAME, authorizationFailureMessages[ key ] ), true );
        }
    }
}

Yeah, this is a little annoying. Maybe someone has a simpler idea, but the solution (hack?) that we came up with was to look for the originally-requested URL that ASP.NET appends to the query string when the user is redirected to the login page.

We created a new web.config section that stores a set of keys/values that match a fragment of the redirect URL to an authorization message:

<configSections>
    <section name="authorizationFailureMessages" type="System.Configuration.NameValueSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    ...etc...
</configSections>

<authorizationFailureMessages>
    <add key="MemberResources" value="MembershipRequired" />
    <add key="Staff" value="StaffOnly" />
    <add key="Departments/Administration/BoardOfDirectors" value="BoardOfDirectorsOnly" />
    ...etc...
</authorizationFailureMessages>

In the Page_Load() event of the Login.aspx page, we call a method that uses this URL to determine which (un)authorization event occurred then redirect them to a message page that displays the appropriate text:

private void DisplayAppropriateAuthorizationMessage ()
{
    if ( !Page.User.Identity.IsAuthenticated )
        return;

    string redirectUrl = FormsAuthentication.GetRedirectUrl( Page.User.Identity.Name, false );

    if ( string.IsNullOrEmpty( redirectUrl ) )
        return;

    NameValueCollection authorizationFailureMessages = ConfigurationManager.GetSection( "authorizationFailureMessages" ) as NameValueCollection;

    if ( authorizationFailureMessages == null )
        return;

    foreach ( string key in authorizationFailureMessages.AllKeys )
    {
        if ( redirectUrl.Contains( key ) )
        {
            Response.Redirect( String.Format( "Message.aspx?{0}={1}", Constants.QueryStringKeys.ERRORMESSAGENAME, authorizationFailureMessages[ key ] ), true );
        }
    }
}
篱下浅笙歌 2024-09-17 18:33:45

我基本上同意@MattPeterson 的解决方案。但我建议进行两项改进。

  1. 在我看来,你只要告诉“根据你的角色,你不可以访问该页面”,就足够了。您无需告知需要哪些额外角色,这将暴露您网站的授权管理细节。

  2. 您可以从web.config(每个文件夹中)获取访问控制列表,无需再次编写。< /p>

应该有类似的内容。

<authorization>
    <deny users="?" />
</authorization

我相信您的 web.config 中

I basically agree to @MattPeterson 's solution. But I suggest two improvements.

  1. In my view, you just tell that "according to the roles that you are, you are not allowed to visit that page", that is enough. You do not need to tell which extra roles are needed, which will expose the details of authorization management of your website.

  2. You can get access control list from web.config (in each folder), and no need to write <add key="MemberResources" value="MembershipRequired" /> again.

I believe you should have something similar to

<authorization>
    <deny users="?" />
</authorization

in your web.config.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文