将Admin Home设置为Forbidden而不使用正确的登录凭据

时间:2013-09-04 18:26:34

标签: asp.net

我正在建立一个网站,我希望管理员主页/用户主页被禁止,而无需使用正确的登录凭据进行验证。但我怎么做。

    protected void btn_Login_Click(object sender, EventArgs e)
    {
        if (CheckFields())
        {
            user.Employee_ID = txtEID.Text;
            user.Employee_Password = txtEPassword.Text;
            user.Role = ddlRole.SelectedItem.ToString();

            if (uc.Login(user)==true && user.Role=="Admin")
            {
                Session["userID"] = user.Employee_ID;
                Response.Redirect("~/Admin/Admin.aspx");
            }
            else if (uc.Login(user) == true && user.Role == "Team Admin")
            {
                Session["userID"] = user.Employee_ID;
                Response.Redirect("TeamAdmin.aspx");
            }
            else if (uc.Login(user) == true && user.Role == "Team Admin")
            {
                Session["userID"] = user.Employee_ID;
                Response.Redirect("TeamMember.aspx");
            }
        }
    }

到此为止它工作正常。但是当我将路径直接设置到网站的管理页面(http://mysite.com/Admin/Admin.aspx)时,它会显示管理页面的所有内容。我需要管理页面禁止b而不验证正确的登录凭据

2 个答案:

答案 0 :(得分:3)

使用以下命令在Admin目录中创建一个web.config文件:

<configuration>
    <system.web>
        <authorization>
            <deny users="?"/>
        </authorization>
    </system.web>
</configuration>

如果您使用某种形式的asp.net身份验证,它将拒绝任何未经过身份验证的用户。

答案 1 :(得分:1)

您需要实施授权(http://msdn.microsoft.com/en-us/library/wce3kxhd.aspx)。

如果您的管理页面位于只有管理员应该有权访问的单独文件夹中,那么您可以将web.config文件放在该文件夹中,其中包含授权部分,如上面的链接所述。您可以将此授权部分设置为仅限制对作为Admin角色成员的用户的访问。为了使其正常工作,您必须使用ASP.Net安全性和授权。

或者,在Admin.aspx页面的Page_Load()方法中,您可以放置​​代码以检查用户是Admins角色的成员。如果不是,请将它们重定向到另一页。

我更喜欢在.config文件中配置页面级别授权,因为它将它保存在一个位置,并允许它在将来轻松更新。