C# “角色”;A「;未找到,并给出System.Configuration.Provider.ProviderException
实际上,我使用以下代码创建了一个名为“a”的角色:C# “角色”;A「;未找到,并给出System.Configuration.Provider.ProviderException,c#,sql,asp.net,C#,Sql,Asp.net,实际上,我使用以下代码创建了一个名为“a”的角色: exec aspnet_Roles_CreateRole @ApplicationName = 'Application name', @RoleName = 'A' 现在我需要将这个角色添加到用户。在我的应用程序中有一个角色列表,以前它与其他角色一起工作,但仍然很好。但未添加此新角色,并引发以下错误: System.Configuration.Provider.ProviderException:未找到角色“A” 这是我的
exec aspnet_Roles_CreateRole
@ApplicationName = 'Application name',
@RoleName = 'A'
现在我需要将这个角色添加到用户。在我的应用程序中有一个角色列表,以前它与其他角色一起工作,但仍然很好。但未添加此新角色,并引发以下错误:
System.Configuration.Provider.ProviderException:未找到角色“A”
这是我的C#代码:
protectedvoidbtnsave\u单击(对象发送方,事件参数e)
{
//找到名字
lblMsg.Visible=false;
namelab.Text=“”;
PrincipalContext ctx=新PrincipalContext(ContextType.Domain);
UserPrincipal qbeUser=新的UserPrincipal(ctx);
首席研究员;
尝试
{
qbeUser.SamAccountName=userTextBox1.Text;
srch=新的PrincipalSearcher(qbeUser);
}
抓住
{
lblMsg.Visible=true;
lblMsg.Text=“员工档案为空!”;
lblMsg.CssClass=“失败”;
返回;
}
foreach(在srch.FindAll()中找到的变量)
{
DirectoryEntry de=found.GetUnderlinegObject()作为DirectoryEntry;
namelab.Text=Convert.ToString(de.Properties[“GivenName”].Value);
principleLabel.Text=Convert.ToString(de.Properties[“UserPrincipalName”].Value);
}
如果(namelab.Text==“”)
{
lblMsg.Visible=true;
lblMsg.Text=“未找到用户名”;
lblMsg.CssClass=“失败”;
返回;
}
列表用户角色列表=新列表();
对于(int i=0;i
我在谷歌上搜索了很多,但没有找到真正的解决方案。你能告诉我哪里出了错吗?在这种情况下,这可能会对你有所帮助 在Microsoft Visual Studio中,转到“网站”并单击“Asp.net配置” 然后在“安全”选项卡中,创建名为
A
的新角色。
从那里,您可以在Asp.net中管理角色分配和用户设置您将角色添加到数据库中,但正在查询AD以获取该角色?您可以告诉我在此之后要执行的过程吗…@非常感谢,此方法帮助了我@拉格万
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";
}