0

我正在使用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> 

请让我知道我做错了吗?

回答

0

您需要改用<deny users="*"/>。请参阅MSDN举例。

+0

它否认了所有角色。不起作用 – 2014-11-06 07:47:55

0

你有试过吗?它会工作

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

我应该在哪里添加? – 2014-11-06 07:52:32

+0

根据您的要求,您可以在getmethod之前或控制器之前放置它 – 2014-11-06 07:58:28

+1

它是'asp.net',而不是'asp.net mvc'没有控制器。 – 2014-11-06 08:05:26

0

我不得不设置formauthentication cookie来做到这一点,一切工作正常,现在

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