找不到角色“A”,并提供System.Configuration.Provider.ProviderException

时间:2015-05-05 05:58:34

标签: c# sql asp.net

实际上我已经使用以下代码创建了一个名为“A”的角色:

exec aspnet_Roles_CreateRole
@ApplicationName  = 'Application name',
@RoleName         = 'A'

现在我需要将此角色添加到用户。我的应用程序中有一个角色列表,之前它正在与其他角色合作,但仍然很好。但是没有添加这个新角色并将错误抛出:

  

System.Configuration.Provider.ProviderException:找不到角色'A'。

这是我的C#代码:

protected void btnSave_Click(object sender, EventArgs e)
{
    //Find Name
    lblMsg.Visible = false;
    nameLabel.Text = "";
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    UserPrincipal qbeUser = new UserPrincipal(ctx);
    PrincipalSearcher srch;

    try
    {
        qbeUser.SamAccountName = userTextBox1.Text;
        srch = new PrincipalSearcher(qbeUser);
    }
    catch
    {
        lblMsg.Visible = true;
        lblMsg.Text = " Employee filed is Empty !  ";
        lblMsg.CssClass = "faild";

        return;
    }
    foreach (var found in srch.FindAll())
    {
      DirectoryEntry de = found.GetUnderlyingObject() as DirectoryEntry;

        nameLabel.Text = Convert.ToString(de.Properties["GivenName"].Value);
        principleLabel.Text = Convert.ToString(de.Properties["UserPrincipalName"].Value);
    }
    if (nameLabel.Text == "")
    {
        lblMsg.Visible = true;
        lblMsg.Text = " Username is not found   ";
        lblMsg.CssClass = "faild";
        return;
    }
    List<string> User_Roles_List = new List<string>();
    for (int i = 0; i < chkSetup.Items.Count; i++)
        if (chkSetup.Items[i].Selected)
            User_Roles_List.Add(chkSetup.Items[i].Value);
    for (int i = 0; i < chkEmployee.Items.Count; i++)
        if (chkEmployee.Items[i].Selected)
            User_Roles_List.Add(chkEmployee.Items[i].Value);
    for (int i = 0; i < chkShift.Items.Count; i++)
        if (chkShift.Items[i].Selected)
            User_Roles_List.Add(chkShift.Items[i].Value);
    for (int i = 0; i < chkAttendance.Items.Count; i++)
        if (chkAttendance.Items[i].Selected)
            User_Roles_List.Add(chkAttendance.Items[i].Value);
    for (int i = 0; i < chkSite.Items.Count; i++)
        if (chkSite.Items[i].Selected)
            User_Roles_List.Add(chkSite.Items[i].Value);
    for (int i = 0; i < chkPayroll.Items.Count; i++)
        if (chkPayroll.Items[i].Selected)
            User_Roles_List.Add(chkPayroll.Items[i].Value);
    for (int i = 0; i < chkOthers.Items.Count; i++)
        if (chkOthers.Items[i].Selected)
            User_Roles_List.Add(chkOthers.Items[i].Value);

    string[] current_roles = Roles.GetRolesForUser(userTextBox1.Text);
    if (current_roles.Length != 0)
        Roles.RemoveUserFromRoles(userTextBox1.Text, current_roles);
    Roles.AddUserToRoles(userTextBox1.Text, User_Roles_List.ToArray());  //Error occur at this line   
    lblMsg.Visible = true;
    lblMsg.CssClass = "successfull";
    lblMsg.Text = "Roles have been successfully updated";
}

我已经google了很多但没有得到实际的解决方案。你可以告诉我我在哪里弄错了吗?

1 个答案:

答案 0 :(得分:1)

在这种情况下,这可能对您有帮助。

在Microsoft Visual Studio中,转到“网站”并单击“Asp.net配置” 然后在“安全”选项卡中,创建名为A的新角色。 从那里,您可以在Asp.net中管理角色分配和用户