我正在使用MVC应用程序中的c#代码来验证用户并查找特定用户所属的用户组列表,并使用以下代码
try
{
List<string> user_groups= new List<string>(); //save the groups the user belongs to
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "mydomain.com",model.Email,model.Password))
{
bool valid=ctx.ValidateCredentials(model.Email, model.Password); //validate the user
if(valid)
{
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, model.Email);
if (user != null)
{
// get the user's groups
var groups = user.GetAuthorizationGroups();
foreach (GroupPrincipal group in groups)
{
// save those groups to session for further processing after login
if ((bool)group.IsSecurityGroup)
{
user_groups.Add(group.Name);
}
}
}
_groups = string.Join(",", user_groups);
ViewBag.Message = _groups;
}
else
{
ViewBag.Message = "Error while validating";
}
ctx.Dispose();
}
}
catch (PrincipalServerDownException)
{
//If server is down or some exception happends ,
// ad_verification = false;
ViewBag.Message = "Error at groups fetching as server is down ";
}
catch (Exception ex)
{
ViewBag.Message = "Error at groups fetching as "+ex.Message;
}我将其部署到服务器,并尝试以user1身份登录,一切都很顺利。代码验证用户凭据,并返回user1所属的用户组列表
现在我在服务器上以user2身份登录,然后它返回以下错误
Error at groups fetching as Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again.看起来我仍然可以作为user1登录,但对于所有其他用户,错误与上面相同。当在本地IIS上测试时,没有任何此类问题,没有任何已知的原因导致第二个用户的上述行中断,以及任何解决此问题的建议
发布于 2019-07-26 20:19:10
您没有说明抛出异常的是哪一行,但您可能不会使用用户的凭据来提取所有数据。
如果您从加入相同域(或受信任域)的计算机上运行此程序,则不需要将凭据放在此处:
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "mydomain.com"))ValidateCredentials()行将验证凭据,其他所有操作都将使用应用程序运行时所使用的凭据来完成。
另外,当ctx是using块的主题时,不需要调用ctx.Dispose()。using的全部目的是在离开using块后调用Dispose()。看看the documentation吧。
https://stackoverflow.com/questions/57218928
复制相似问题