【问题标题】:Location based Role authentication not working基于位置的角色身份验证不起作用
【发布时间】:2014-11-06 07:40:24
【问题描述】:

我正在开发一个带有 sql 成员资格提供程序的 Web 应用程序。 我已经在 SQL 中为用户映射了角色,并且用户被正确分配给角色。 以下代码工作正常。

        protected void btnLogin_Click(object sender, EventArgs e)
        {
        if (Membership.ValidateUser(txtUserName.Text, txtPassWord.Text))
        {
            if (Roles.IsUserInRole(txtUserName.Text, "admin"))
            Response.Redirect("~/Users/ViewUsers.aspx");
        }
        else
        {
            lblErrorMessage.Visible = true;
        }
    }

但我想在我的配置中执行所有访问拒绝逻辑。 以下代码不起作用。具有所有角色的用户无论其角色如何都会被重定向。

<location path="Users">
<system.web>
  <authorization>
    <allow roles="admin"/>
    <deny roles="user"/>
  </authorization>
</system.web>

请让我知道我做错了什么?

【问题讨论】:

    标签: c# web-config authorization asp.net-membership asp.net-roles


    【解决方案1】:

    您需要改用&lt;deny users="*"/&gt;。请参阅MSDN 文章的示例。

    【讨论】:

      【解决方案2】:

      你试过这个吗?它会工作

       [Authorize(Roles = "Super Admin,Business Admin")]
      

      【讨论】:

      • 我应该在哪里添加这个?
      • 你可以根据你的要求把它放在getmethod之前或者controller类之前
      • asp.net,不是asp.net mvc,没有控制器。
      【解决方案3】:

      我必须设置formauthentication cookie才能做到这一点,现在一切正常

      string username = UsernameTB.Text;
      FormsAuthentication.SetAuthCookie(username, false);
      

      【讨论】:

        猜你喜欢
        • 2013-05-29
        • 2020-03-13
        • 2020-12-21
        • 2016-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-19
        • 1970-01-01
        相关资源
        最近更新 更多