1

请参考这篇文章。

我已经能够配置我的web.config文件,以便当未经身份验证的用户请求页面时,他会被重定向到该Login.aspx页面。

我已经能够通过配置 web.config 文件和以下几行代码来做到这一点:

protected void btnLogin_Click(object sender, EventArgs e)
        {
            string username = this.usernameTextBox.Text;
            string password = this.passwordTextBox.Text;

            bool success = Membership.ValidateUser(username.Trim(), password.Trim());

            if (success)
            {
                FormsAuthentication.SetAuthCookie(username, true);

                Ice_Web_Portal.BO.User user = Ice_Web_Portal.BO.User.GetUserByUserName(username);

                Ice_Web_Portal.BO.UserTypeEnum loginUserType = user.UserTypeEnum;

                if (loginUserType == UserTypeEnum.Student)
                {
                    Response.Redirect("~/Student/StudentControlPanel.aspx?username=" + username);
                }
                else if (loginUserType == UserTypeEnum.Teacher)
                {
                    Response.Redirect("~/Teacher/TeacherControlPanel.aspx?username=" + username);
                }
                else if(loginUserType == UserTypeEnum.Webmaster)
                {
                    Response.Redirect(@"~/Webmaster/WebmasterControlPanel.aspx");
                }
                else
                {
                    labLoginMessage.Text = "Sorry! Type of user couldn't be determined!";
                }
            }
            else
            {
                labLoginMessage.Text = Ice_Web_Portal.BO.User.LoginMessage;
            }
        }

但是我遇到的问题是,一旦用户通过身份验证,他就可以访问整个 Web 应用程序中的所有页面。

但我需要根据他们的角色限制他们的页面访问区域。即当具有不同角色的用户请求一个页面时,他应该被自动重定向到该Login.aspx页面。

可能有一种技术,我可以在其中检查特定的用户角色,然后如果用户不在该角色中,Page_Load()-event则将用户重定向到页面。Login.aspx但我不想那样做。我想自动发生。我只需要使用 Role Provider 框架和 web.config 文件(就像在成员资格的情况下一样。即我不需要检查 Page_Load 事件中的成员资格。Web.config 文件会自动阻止访问)。

谁能告诉我如何在其中加入角色功能,以便特定用户被限制在他们特定的角色区域内?

生成授权票的代码是什么?

4

3 回答 3

4

将部分添加到 web.config

  <location path="page-only-allowed-to-be-accessed-by-admin.aspx">
      <system.web>
         <authorization>
           <allow roles="admin"/>
           <deny users="*" />
         </authorization>
      </system.web>
   </location>

您可能会发现这篇文章很有趣 -揭秘 web.config

编辑:

生成授权票的代码在您的代码中。

FormsAuthentication.SetAuthCookie(username, true);

是这样实现的(使用Red Gate 的 Reflector

public static void SetAuthCookie(string userName, bool createPersistentCookie, string strCookiePath)
{
    Initialize();
    HttpContext current = HttpContext.Current;
    if (!current.Request.IsSecureConnection && RequireSSL)
    {
        throw new HttpException(SR.GetString("Connection_not_secure_creating_secure_cookie"));
    }
    bool flag = CookielessHelperClass.UseCookieless(current, false, CookieMode);
    HttpCookie cookie = GetAuthCookie(userName, createPersistentCookie, flag ? "/" : strCookiePath, !flag);
    if (!flag)
    {
        HttpContext.Current.Response.Cookies.Add(cookie);
        current.CookielessHelper.SetCookieValue('F', null);
    }
    else
    {
        current.CookielessHelper.SetCookieValue('F', cookie.Value);
    }
}

RoleProvider 将获取给定用户的角色,因此当检查 web.config 以获取应用程序给定部分的允许或拒绝角色/用户时,RoleProvider 将获取用户的角色,然后检查允许/拒绝角色并在适当时授权。

于 2009-09-03T10:08:01.010 回答
2

使用角色提供者。

设置角色提供程序并为用户分配角色后,您可以使用<authorization>Web.config 部分根据角色成员资格限制对各种资源的访问。

如果您有可用的 SQL Server,我建议您使用 SqlRoleProvider。它非常灵活,因为它可以为用户名分配角色,而无需先注册用户 - 具体来说,您不需要同时使用 SqlMembershipProvider(或者实际上是任何成员资格提供程序)。IE。如果将角色“Student”添加到用户名“John”,SqlRoleProvider 将简单地将角色与该用户名相关联,并且一切正常。

祝你好运!

于 2009-09-03T10:09:19.277 回答
0

如果文件夹中有一组受限文件,则可以在 web.config 中将角色限制为该文件夹:

例如:

<location path="TeacherAdmin" allowOverride="false">        
  <system.web>
    <authorization>
      <allow roles="Teacher"/>
      <deny users="*,?"/>           
    </authorization>        
  </system.web>     
</location>

注意:path属性也可以指向特定的aspx页面

于 2009-09-03T10:07:17.427 回答