如何检测哪个Windows帐户正在运行.net应用程序?

时间:2010-04-27 21:37:13

标签: c# .net sharepoint

我正在写一个sharepoint web部分。它将日志写入文件(使用StreamWriter)。但是,仅为帐户是托管Web部件的服务器上的管理员的用户编写日志。我想检测哪个帐户(可能不是通过使用SPUser)正在执行Web部件的代码,以便我可以为较少特权的用户生成日志。这可能吗?

由于

3 个答案:

答案 0 :(得分:1)

Framework提供了一个WindowsIdentity类,它表示经过身份验证的Windows用户和一个WindowsPrincipal类,它封装了WindowsIdentity以及有关用户角色成员身份的信息。

答案 1 :(得分:1)

此代码段可以为您提供用户所属的群组。

var id = System.Security.Principal.WindowsIdentity.GetCurrent();
IdentityReferenceCollection irc = id.Groups;
foreach (IdentityReference ir in irc)
     Console.WriteLine(ir.Value);

答案 2 :(得分:1)

有一些方法可以达到相同的效果:

  • Request.LogonUserIdentity
  • HttpContext.Request.LogonUserIdentity
  • Page.User.Identity

我认为那里的一个必须在Sharepoint服务器上运行。哪个将工作取决于您想要的上下文包括代码,您应该在您需要的地方放置一个断点,并在观察窗口中看到上面的构造之一。如果其中一个工作,您将收到System.Security.Principal.WindowsPrincipal类型的对象,它可以为您提供所需的一切。例如,Name属性是用户名。 Groups属性是SecurityIdentifier的列表,它对应于用户所属的组。可以使用NTAccount类将SecurityIdentifier转换为name。 (例如,参见I have a SID of a user account, and I want the SIDs of the groups it belongs to