【问题标题】:How to get Identity User Data from Asp.net mvc Model如何从 Asp.net mvc 模型中获取身份用户数据
【发布时间】:2014-04-25 09:12:47
【问题描述】:

我有一个模型如下:

public class BaseModel 
{
    public DateTime? CrDate { get; set; }

    [ForeignKey("CrUser")]
    public ApplicationUser UserCr { get; set; }
    public string CrUser { get; set; }

    public DateTime? MdDate { get; set; }

    [ForeignKey("MdUser")]
    public ApplicationUser UserMd { get; set; }
    public string MdUser { get; set; }


    public bool IsDeleted { get; set; }



    public void LogWhatever()
    {
        this.CrDate = System.DateTime.Now;
        this.CrUser = ?????
    }

}

如何从模型中获取记录的用户 ID 以存储在 CrUser 中?

问候。

【问题讨论】:

    标签: asp.net-mvc identity


    【解决方案1】:

    Asp.net Identity 暴露 IUser(IdentityUser) 上下文,一旦用户登录。

    Context.User.Identity.GetUserId()
    

    【讨论】:

    • 我应该只写 Context.User.Identity.GetUserId() 吗?它不工作,因为没有找到上下文?
    • HttpContext.Current.User.Identity.GetUserId();是正确的答案吗?这解决了我的问题,但这是个好方法吗?
    • 你能在你的模型中访问 HttpContext 吗?这是应用程序应该如何使用用户上下文的确切方式/方法。让我知道我会去看看。
    猜你喜欢
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多