2013-02-27 66 views
0

我的要求是 1)創建角色並將用戶添加到該特定角色。 2)我有一個目錄管理員,其中包含一個aspx頁面,只能由管理員訪問 3)所以我想重定向用戶登錄頁面,如果他試圖訪問任何一個aspx頁面到文件夾。有關Asp.net會員資格的查詢

實現這個

我必須使用Asp.net會員創建用戶和角色以及各種功能的 做一個登錄表單。 問題是每次我需要打開asp.net配置來創建一個新用戶並將該用戶分配給一個特定的角色。 如果我在網站上部署我的網站。所以我現在如何添加用戶。

我列出我的網絡配置文件

<configuration> 
    <connectionStrings> 
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=true;Initial Catalog=Web24;" providerName="System.Data.SqlClient"/> 
    </connectionStrings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0"/> 
    <authentication mode="Forms"> 
     <forms loginUrl="~/WebForm1.aspx" timeout="2880"/> 
    </authentication> 
    <membership> 
     <providers> 
     <clear/> 
     <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/> 
     </providers> 
    </membership> 
    <profile> 
     <providers> 
     <clear/> 
     <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/> 
     </providers> 
    </profile> 
    <roleManager enabled="true"> 
     <providers> 
     <clear/> 
     <add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider"/> 
     <add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider"/> 
     </providers> 
    </roleManager> 

    </system.web> 
    <location path="Admin"> 
    <system.web> 
    <authorization> 
     <deny users="?" /> 
     <!--Deny all Anonymous (not logged in) users--> 
     <allow roles="Admin"/> 
     <!--Permit users in these roles--> 
     <deny users="*"/> 
     <!--Deny all users--> 
    </authorization> 
    </system.web> 
    </location> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

,如果它是不可能的Asp.net會員的話,我應該遵循什麼其他的方法來掩蓋我requirement.Thanks的任何援助。

+0

它很可能 - 你只需要在您的管理區域編寫用於創建用戶的用戶界面,用戶以代碼形式提供成員資格提供程序(而不是使用實用程序)。 – 2013-02-27 09:29:02

回答

1

您可以創建一個管理頁面,從那裏他可以將用戶添加類似

ASPX

<tr> 
     <td> 
      Username:</td> 
     <td> 
      <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      Password:</td> 
     <td> 
      <asp:TextBox ID="txtUserPass" runat="server" TextMode="Password"></asp:TextBox> 
     </td> 
    </tr> 

    <tr> 
     <td valign=top> 
      Roles:</td> 
     <td> 
      <asp:CheckBoxList ID="cblRoles" runat="server"> 
       <asp:ListItem>Admin</asp:ListItem> 
       <asp:ListItem>Role 1</asp:ListItem> 
       <asp:ListItem>Role 2</asp:ListItem> 
      </asp:CheckBoxList></td> 
    </tr> 
    <tr> 
     <td> 
      &nbsp;</td> 
     <td> 
      <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" /> 
     </td> 
    </tr> 

aspx.cs

protected void btnSave_Click(object sender, EventArgs e) 
    { 
     MembershipCreateStatus createStatus; 
     MembershipUser newUser = Membership.CreateUser(txtUserName.Text, txtUserPass.Text, null, null, null, true, out createStatus); 

     if (newUser != null) 
     { 
      foreach (ListItem li in cblRoles.Items) 
      { 
       if (li.Selected) 
       { 
        Roles.AddUserToRole(txtUserName.Text, li.Value.ToString()); 
       } 
      } 
     } 
    } 
+0

謝謝,先生,它現在的作品。 – 2013-02-27 09:47:58

+0

永遠歡迎:) – ZedBee 2013-02-27 09:55:52

1

你可以在創建用戶表單中使用它。它可能通過此代碼添加用戶名和密碼

試一試。如果你有任何疑問給我寫信。

Membership.CreateUser("Username", "Password"); 
      Roles.AddUserToRole("username", "RoleName");