User.Identity.Name - which assembly reference brings that into your project?

那年仲夏 提交于 2019-12-07 19:49:38

问题


I can't find out which reference assembly the User.Identity.Name comes from. How do I find that out? I need to add it to a class library rather than the default MVC 5 app I have made so that I can have access to it?

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

C# Class Library / AUser.cs

public class AUser
{
    public string Username
    {
        get
        {
            return User.Identity.Name // doesn't notice User
        }
    }
}

回答1:


Appears to be in the System.Web namespace.

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

EDIT

Based on your additional code, you have to include the fully qualified name - such as:

HttpContext.Current.User.Identity.Name

When you are outside of a controller (or in another class). Be aware that if this outside of MVC class is used in anything without an HttpContext, it will fail (with a NullReferenceException). To ensure we don't have any of those, we typically either pass the HttpContext or the portions we need (Username, Url, etc) as function parameters.



来源:https://stackoverflow.com/questions/20672127/user-identity-name-which-assembly-reference-brings-that-into-your-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!