2012-05-15 86 views
0

我有一個包含2個文件夾的Web應用程序。管理員和培訓師,其中包含各自的頁面。我在每個文件夾中都有一個web.config,如下所示。當我使用這些配置設置登錄時,用戶被拒絕訪問他的主頁,如果我刪除拒絕用戶,每個人都可以登錄。我已經創建角色並使用WSAT將用戶添加到角色中。允許角色不起作用

的Web.config管理員

<?xml version="1.0"?> 
<configuration> 
<system.web> 
    <authorization> 
     <allow roles="Administrator" /> 
     <deny users="?"/> 
    </authorization> 
</system.web> 
</configuration> 

的Web.config教練

<?xml version="1.0"?> 
<configuration> 
<system.web> 
    <authorization> 
     <allow roles="Trainer" /> 
     <deny users="?"/> 
    </authorization> 
</system.web> 
</configuration> 

根文件夾的web.config文件

<?xml version="1.0"?> 

<configuration> 
<connectionStrings> 
<add name="TSS" connectionString="Data Source = VC-SQL2008; Integrated 
    Security=True; database = aspnetdb" providerName="System.Data.SqlClient"/> 
</connectionStrings> 

<system.web> 
<compilation debug="true" targetFramework="4.0"/> 
<authentication mode="Forms"> 
    <forms loginUrl="Login.aspx" timeout="2880" /> 
</authentication> 
</system.web> 

<system.web> 
<membership> 
    <providers> 
    <clear/> 
    <add name="AspNetSqlMembershipProvider" 
type="System.Web.Security.SqlMembershipProvider" connectionStringName="TSS" 
requiresQuestionAndAnswer="false" requiresUniqueEmail="false" 
enablePasswordRetrieval="false" enablePasswordReset="false" 
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="1" 
minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" 
applicationName="/"/> 

    </providers> 
    </membership> 

    <profile> 
    <providers> 
    <clear/> 
    <add name="AspNetSqlProfileProvider" 
    type="System.Web.Profile.SqlProfileProvider" 
    connectionStringName="TSS" applicationName="/"/> 
    </providers> 
</profile> 

<roleManager enabled="true"> 
    <providers> 
    <clear /> 
    <add connectionStringName="TSS" applicationName="/" name="AspNetSqlRoleProvider" 
    type="System.Web.Security.SqlRoleProvider" /> 
    <!--<add applicationName="/" name="AspNetWindowsTokenRoleProvider" 
     type="System.Web.Security.WindowsTokenRoleProvider" />--> 
    </providers> 
</roleManager> 

<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/> 
</system.web> 

<system.webServer> 
<modules runAllManagedModulesForAllRequests="true"/> 
</system.webServer> 
</configuration> 

對我如何添加的角色

enter<siteMapNode url="Administrator/Admin_Home.aspx" title="Home" description="" 
roles="Administrator"> 

Login.aspx.cs 命名空間TSS { 公共部分類Login2身份的web.sitemap例如:System.Web程序。 UI.Page {0}保護無效Page_Load(object sender,EventArgs e) { dbConnection dbConn = new dbConnection(); }

protected void submit_Click(object sender, EventArgs e) 
    { 
     // var a = Session["username"]; 
     string password = tb_password.Text; 
     // Membership.CreateUser("[email protected]", "9000"); 

     bool x = Membership.ValidateUser(tb_email.Text, password); 
     string f_name; 
     string l_name; 
     string trainer=""; 
     DataTable dt = new DataTable(); 
     dt = TSS_WebService.getEmployeeByEmail(tb_email.Text); 

     foreach (DataRow row in dt.Rows) 
     { 
      f_name = row["First_Name"].ToString(); 
      l_name = row["Last_Name"].ToString(); 
      trainer = row["First_Name"].ToString() + " " + 
      row["Last_Name"].ToString(); 
     } 

    if (x == true) 
    { 

    Session["username"] = tb_email.Text; 
    Session["trainer"] = trainer; 

    if (Roles.IsUserInRole(tb_email.Text, "Administrator")) 
    { 
     Response.Redirect("~/Administrator/Admin_Home.aspx"); 
    } 

    if (Roles.IsUserInRole(tb_email.Text, "Trainer")) 
    { 

    Response.Redirect("~/Trainer/Trainer_Home.aspx"); 
    } 

    if (Roles.IsUserInRole(tb_email.Text, "Salon Manager")) 
    { 

    Response.Redirect("~/Salon/Salon_Home.aspx"); 
    } 

    if (Roles.IsUserInRole(tb_email.Text, "IT")) 
    { 

    Response.Redirect("Home.aspx"); 
    } 
    } 

    else 
    { 
     FormsAuthentication.RedirectToLoginPage(); 
    } 
    } 

    } 
    } 


***Login.aspx*** 
    <%@ Page Title="" Language="C#" MasterPageFile="~/Master/Master.Master"  
    AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="TSS.Login2" %> 
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
    </asp:Content> 
    <asp:Content ID="Content2" ContentPlaceHolderID="BreadCrumbs" runat="server"> 
    <asp:SiteMapPath ID="SiteMapPath1" runat="server"> 
    </asp:SiteMapPath> 
    </asp:Content> 
    <asp:Content ID="Content3" ContentPlaceHolderID="MainArea" runat="server"> 
    <div id = "loginBox"> 
    <h2> LOGIN</h2> 
    <asp:TextBox ID="tb_email" runat="server" class = "ipBox_large"></asp:TextBox><br 
    /> 
    <asp:TextBox ID="tb_password" runat="server" class = "ipBox_large"></asp:TextBox> 
    <br /> 
    <asp:ImageButton ID= "btn" ImageUrl = "../Images/btnLogin.gif" OnClick = 
    "submit_Click" 
    runat="server" /> 
    <asp:CheckBox id="NotPublicCheckBox" runat="server" /> 
    </div> 
    </asp:Content> 

我一直堅持這個2天現在已經研究一切可能我幫助could.Any或建議,非常感謝感謝。

+0

角色提供者怎麼樣?它是否啓用了該網站? –

+0

請看我更新的答案。 – jams

+0

我在我的web.config中啓用了角色管理器爲true。有什麼我失蹤?我不確定角色提供者。 – user1288906

回答

0

嘗試,而不是你if(x==true){...}部分下面的代碼,如果(X ==真){ 如果 (的Request.QueryString [ 「RETURNURL」]!= NULL){ //重定向到返回URL FormsAuthentication.RedirectFromLoginPage(userName.Text,NotPublicCheckBox.Checked); }

/* create authentication cookie */ 
    FormsAuthentication.SetAuthCookie(tb_email.Text, NotPublicCheckBox.Checked) 
    Session["username"] = tb_email.Text; 
    Session["trainer"] = trainer; 

    /*redirect depending on roles*/ 
    if (Roles.IsUserInRole(tb_email.Text, "Administrator")) 
    { 
     Response.Redirect("~/Administrator/Admin_Home.aspx"); 
    } 

    if (Roles.IsUserInRole(tb_email.Text, "Trainer")) 
    { 
     Response.Redirect("~/Trainer/Trainer_Home.aspx"); 
    } 

    if (Roles.IsUserInRole(tb_email.Text, "Salon Manager")) 
    { 
     Response.Redirect("~/Salon/Salon_Home.aspx"); 
    } 

    if (Roles.IsUserInRole(tb_email.Text, "IT")) 
    { 
     Response.Redirect("Home.aspx"); 
    } 
} 
else 
{ 
    /*Login error*/ 
    FormsAuthentication.RedirectToLoginPage(); 
} 

希望它的工作原理。祝你好運。

+0

非常感謝您的幫助。這對我來說非常有用。再次感謝! – user1288906

2

使用<deny users="?"/> 而不是<deny users="*"/>

+0

我試過了,它沒有工作!有什麼我可能錯過了我的代碼隱藏,網站地圖或web.config文件? – user1288906

+0

拒絕用戶?,*並允許角色「管理員」。這將防止管理員以外的任何人訪問該文件夾。 admin文件夾將從父配置繼承允許角色「Trainers」,?,*將會阻止它。 –

+0

@Nick Bork ::我認爲user1288906錯誤地輸入了問題。因爲我們在任何目錄中都不能有兩個web.config。在這裏他提供了兩個根目錄web.config編碼的問題。 –

相關問題