User.Identity.Name - 哪个程序集引用将其带入您的项目? [英] User.Identity.Name - which assembly reference brings that into your project?

查看:43
本文介绍了User.Identity.Name - 哪个程序集引用将其带入您的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找出 User.Identity.Name 来自哪个参考程序集.我怎么知道?我需要将它添加到类库而不是我制作的默认 MVC 5 应用程序中,以便我可以访问它?

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# 类库/AUser.cs

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

推荐答案

似乎在 System.Web 命名空间中.

Appears to be in the System.Web namespace.

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

编辑

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

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

HttpContext.Current.User.Identity.Name

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆