User.Identity.Name - 哪个程序集引用将其带入您的项目?

Jim*_*988 3 c# windows-authentication .net-assembly visual-studio-2013 asp.net-mvc-5

我找不到User.Identity.Name来自哪个引用程序集.我怎么找到它?我需要将它添加到类库而不是我已经创建的默认MVC 5应用程序,以便我可以访问它吗?

http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user(v=vs.110).aspx

C#类库/ AUser.cs

public class AUser
{
    public string Username
    {
        get
        {
            return User.Identity.Name // doesn't notice User
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Tom*_*mmy 9

似乎位于System.Web命名空间中.

http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user(v=vs.110).aspx

编辑

根据您的附加代码,您必须包含完全限定的名称 - 例如:

HttpContext.Current.User.Identity.Name
Run Code Online (Sandbox Code Playgroud)

当你在控制器之外(或在另一个类中).请注意,如果在没有HttpContext的情况下在MVC类之外使用它,它将失败(使用NullReferenceException).为了确保我们没有这些,我们通常会将HttpContext或我们需要的部分(Username,Url等)作为函数参数传递.