【问题标题】:User.Identity.Name - which assembly reference brings that into your project?User.Identity.Name - 哪个程序集引用将它带入您的项目?
【发布时间】:2013-12-19 01:58:44
【问题描述】:

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

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
        }
    }
}

【问题讨论】:

    标签: c# windows-authentication visual-studio-2013 .net-assembly asp.net-mvc-5


    【解决方案1】:

    似乎位于 System.Web 命名空间中。

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

    编辑

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

    HttpContext.Current.User.Identity.Name
    

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

    【讨论】:

    • @JamesT - 让我看看我是否可以挖掘其他任何东西,也许是一个子依赖程序集。你想怎么称呼它?
    • 调整了 OP,希望对您有所帮助
    • @JamesT - 随时! MvcController/View 基类已经引用了 HttpContext.Current,这就是为什么您不需要在控制器或视图中指定所有这些。
    猜你喜欢
    • 2010-11-11
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 2022-06-13
    相关资源
    最近更新 更多