User.Identity.Name - 这集引用这带来到您的项目? [英] User.Identity.Name - which assembly reference brings that into your project?
问题描述
我无法找出引用装配在 User.Identity.Name 从何而来。我怎么发现的?我需要将它添加到一个类库,而不是默认的MVC应用5我做,这样我可以访问它?
的http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user(v=vs.110).aspx
C#类库/ AUser.cs
公共类AUser
{
公共字符串用户名
{
得到
{
返回User.Identity.Name //不会注意到用户
}
}
}
似乎是在System.Web命名空间。
的http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user(v=vs.110).aspx
修改
根据您的额外code,你必须包括完全合格的名字 - 如:
HttpContext.Current.User.Identity.Name
当你在室外控制器的(或在另一个类)。请注意,如果这个MVC类以外的任何东西使用没有HttpContext的,它会失败(有一个NullReferenceException)。为了确保我们没有任何这些,我们通常是通过HttpContext的还是我们所需要的部分(用户名,网址等)作为函数的参数。
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
}
}
}
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.
这篇关于User.Identity.Name - 这集引用这带来到您的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!