4

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
        }
    }
}
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233

1 Answers1

10

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.

Tommy
  • 39,592
  • 10
  • 90
  • 121
  • @JamesT - let me see if I can dig up anything else, maybe a sub-dependent assembly. How are you trying to call it? – Tommy Dec 19 '13 at 02:01
  • tweaked the OP, hope it helps – Jimmyt1988 Dec 19 '13 at 02:01
  • 1
    @JamesT - Anytime! The MvcController/View base class already has a reference to the HttpContext.Current, which is why you don't need to specify all of that in a controller or view. – Tommy Dec 19 '13 at 02:06