Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
        }
    }
}
like image 558
Jimmyt1988 Avatar asked Dec 26 '22 16:12

Jimmyt1988


1 Answers

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.

like image 193
Tommy Avatar answered May 05 '23 06:05

Tommy