获取当前用户的SID的最佳方法是什么?

Max*_*ing 15 .net c# asp.net

先决条件细节

  1. 在.NET 2.0中工作.
  2. 代码位于可以从ASP.Net,Windows窗体或控制台应用程序调用的公共库中.
  3. 在公司网络上的Windows域中运行.

问题

获取当前用户的SID的最佳方法是什么?我不是在谈论正在执行应用程序的身份,而是正在访问界面的用户.在后台应用程序和基于桌面的应用程序中,这应该是实际执行应用程序的标识,但在ASP.Net(没有版本化)中,这应该是HttpContext.Current.User SID.

现行方法

这就是我现在所拥有的.它似乎......错了.这很讨厌.有没有更好的方法来做到这一点,或者一些内置的类为你做的?

public static SecurityIdentifier SID
{
    get
    {
        WindowsIdentity identity = null;

        if (HttpContext.Current == null)
        {
            identity = WindowsIdentity.GetCurrent();
        }
        else
        {
            identity = HttpContext.Current.User.Identity as WindowsIdentity;
        }

        return identity.User;
    }
}
Run Code Online (Sandbox Code Playgroud)

Wya*_*ett 5

我认为没有更好的方法来获取此信息 - 您必须以某种方式获取 WindowsPrincipal,而 .NET 相当干净的 API 抽象了 User 对象背后的内容。我只是把这个美好的事情保留在一个方法中,然后就到此为止了。

好吧,您应该做一件事(除非您的 Web 用户始终是 WindowsIdentity),即检查空身份并根据您拥有的任何规则进行处理。