Asp.net mvc User.Identity.GetUserId()的负担是什么?

Asp.net mvc User.Identity.GetUserId()的负担是什么?,asp.net-mvc,asp.net-membership,Asp.net Mvc,Asp.net Membership,在我的ASP.NET MVC应用程序中,我大量使用User.Identity.GetUserId()。但是,我想知道这是否会造成严重的性能损失 或者,我相信我可以做到这一点:在视图中,我可以将当前用户的id分配给第一个页面加载中的隐藏字段。然后,在进行AJAX调用时,我可以将隐藏字段值传递给控制器的操作。这样,我就不需要使用User.Identity.GetUserId()方法来检索当前用户的userid 我想知道是否有人对此有什么想法?看看GetUserId扩展方法的源代码: /// <

在我的ASP.NET MVC应用程序中,我大量使用
User.Identity.GetUserId()
。但是,我想知道这是否会造成严重的性能损失

或者,我相信我可以做到这一点:在视图中,我可以将当前用户的id分配给第一个页面加载中的隐藏字段。然后,在进行AJAX调用时,我可以将隐藏字段值传递给控制器的操作。这样,我就不需要使用
User.Identity.GetUserId()
方法来检索当前用户的userid


我想知道是否有人对此有什么想法?

看看
GetUserId
扩展方法的源代码:

/// <summary>
///     Return the user id using the UserIdClaimType
/// </summary>
/// <param name="identity"></param>
/// <returns></returns>
public static string GetUserId(this IIdentity identity)
{
    if (identity == null)
    {
        throw new ArgumentNullException("identity");
    }
    var ci = identity as ClaimsIdentity;
    if (ci != null)
    {
        return ci.FindFirstValue(ClaimTypes.NameIdentifier);
    }
    return null;
}

/// <summary>
///     Return the claim value for the first claim with the specified type if it exists, null otherwise
/// </summary>
/// <param name="identity"></param>
/// <param name="claimType"></param>
/// <returns></returns>
public static string FindFirstValue(this ClaimsIdentity identity, string claimType)
{
    if (identity == null)
    {
        throw new ArgumentNullException("identity");
    }
    var claim = identity.FindFirst(claimType);
    return claim != null ? claim.Value : null;
}
您还可以创建一个服务来封装该信息

private string userId
public string UserId {
    get {
        if(userid == null) {
            userid = User.Identity.GetUserId();
        }
        return userid;
    }
}