【问题标题】:How to detect which Windows account is running a .net application?如何检测哪个 Windows 帐户正在运行 .net 应用程序?
【发布时间】:2010-04-28 06:54:06
【问题描述】:

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

谢谢

【问题讨论】:

    标签: c# .net sharepoint


    【解决方案1】:

    这个 sn-p 将为您提供用户所属的组。

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

    【讨论】:

      【解决方案2】:

      框架提供了一个代表经过身份验证的 Windows 用户的 WindowsIdentity 类和一个封装了 WindowsIdentity 和有关用户角色成员身份信息的 WindowsPrincipal 类。

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-24
          • 1970-01-01
          相关资源
          最近更新 更多