【问题标题】:How do I get all Roles for current User?如何获取当前用户的所有角色?
【发布时间】:2014-03-09 00:42:59
【问题描述】:

我正在为 WPF 应用程序开发业务逻辑。在应用程序启动之前,我必须证明 CurrentUser 是否在一个特殊域的 ActiveDirectory 中,如果他在那里,我必须找出当前用户的角色。当前用户和他在 AD 中的存在工作正常,但我很难找出角色。

我试过了:

using System.Web.Security;

Roles.GetRolesForUser(currentuser);

但问题是,我必须启用角色管理,所以我在代码上方写了:

Roles.Enabled = true;

但是还是有问题 --> System.InvalidOperationException;

这是整个代码(测试版):

string currentuser = Environment.UserName;
string currentmachine = Environment.MachineName;

if (DirectoryEntry.Exists(string.Format("WinNT://{0}/{1}", currentmachine,  currentuser)))
{
    Console.WriteLine("it's working\n");


    Roles.Enabled = true;
    Console.WriteLine(Roles.GetRolesForUser(currentuser));
}
else
{
    Console.WriteLine("it's not working");
}

是 using 指令的问题吗?或者是否有其他可能性来检查用户的角色?

非常感谢。

【问题讨论】:

    标签: c# wpf active-directory roles


    【解决方案1】:

    如果您也在使用会员资格,请尝试此操作

    var user = Membership.GetUser("WhateverUsername");
    string [] roles = Roles.GetRolesForUser(user.UserName);
    

    如果不是,那么只需指定用户名:

    string [] roles = Roles.GetRolesForUser("WhateverUsename");
    

    这将获得您提供的用户名的角色。希望对您有所帮助。

    【讨论】:

    • System.InvalidOperationException;还是有问题
    • 异常说:This method can only be called during the initialization phase before the apllication starts.
    • 尝试在配置文件的 appSettings 区域添加这些
    【解决方案2】:

    也许这可以帮助你:Active Directory - Roles of a user

    在这篇文章中使用了 System.DirectoryServices.AccountManagement-Namespace。 我认为这是 WPF-Application 和 Roles.GetRolesForUser 似乎用于 ASP-Web-Applications 的更好方法。

    希望对你有帮助。

    【讨论】:

    • 我正在使用这个指令:using System.DirectoryServices.AccountManagement; using System.DirectoryServices.ActiveDirectory; using System.DirectoryServices.Protocols; using System.DirectoryServices;
    • 谢谢您 - 必须尝试使用​​该域进行一些操作,但现在它运行良好。
    猜你喜欢
    • 2016-04-17
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2018-11-07
    相关资源
    最近更新 更多