User.Identity.GetUserId是否已缓存或每次从数据库中获取?

时间:2015-02-14 20:49:56

标签: c# asp.net-mvc asp.net-mvc-5 asp.net-identity

我正在使用ASP.Net MVC 5而我在我的代码中调用此行

string userId = User.Identity.GetUserId();

ASP.NET MVC是否会从表中为每次调用获取此内容,还是会缓存?

2 个答案:

答案 0 :(得分:7)

查看Microsoft.AspNet.Identity.Core.dll的反编译源,您可以看到它从当前标识的声明中检索用户标识。因此它不会从数据库中获取此信息。

public static string GetUserId(this IIdentity identity)
{
  if (identity == null)
    throw new ArgumentNullException("identity");
  ClaimsIdentity identity1 = identity as ClaimsIdentity;
  if (identity1 != null)
    return IdentityExtensions.FindFirstValue(identity1, "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");
  return (string) null;
}

答案 1 :(得分:4)

登录时似乎缓存了idusername

我使用了 SQL Server Profiler ,登录后运行User.Identity.GetUserId();身份系统对数据库进行了无查询

这是在loggin上进行的查询:

exec sp_executesql N'SELECT 
    [UnionAll2].[C2] AS [C1], 
    [UnionAll2].[C3] AS [C2], 
    [UnionAll2].[C4] AS [C3], 
    [UnionAll2].[C5] AS [C4], 
    [UnionAll2].[C6] AS [C5], 
    [UnionAll2].[C7] AS [C6], 
    [UnionAll2].[C8] AS [C7], 
    [UnionAll2].[C9] AS [C8], 
    [UnionAll2].[C10] AS [C9], 
    [UnionAll2].[C11] AS [C10], 
    [UnionAll2].[C12] AS [C11], 
    [UnionAll2].[C13] AS [C12], 
    [UnionAll2].[C14] AS [C13], 
    [UnionAll2].[C1] AS [C14], 
    [UnionAll2].[C15] AS [C15], 
    [UnionAll2].[C16] AS [C16], 
    [UnionAll2].[C17] AS [C17], 
    [UnionAll2].[C18] AS [C18], 
    [UnionAll2].[C19] AS [C19], 
    [UnionAll2].[C20] AS [C20], 
    [UnionAll2].[C21] AS [C21], 
    [UnionAll2].[C22] AS [C22], 
    [UnionAll2].[C23] AS [C23], 
    [UnionAll2].[C24] AS [C24], 
    [UnionAll2].[C25] AS [C25]
    FROM  (SELECT 
        [UnionAll1].[C1] AS [C1], 
        [UnionAll1].[AccessFailedCount] AS [C2], 
        [UnionAll1].[Id] AS [C3], 
        [UnionAll1].[Email] AS [C4], 
        [UnionAll1].[EmailConfirmed] AS [C5], 
        [UnionAll1].[PasswordHash] AS [C6], 
        [UnionAll1].[SecurityStamp] AS [C7], 
        [UnionAll1].[PhoneNumber] AS [C8], 
        [UnionAll1].[PhoneNumberConfirmed] AS [C9], 
        [UnionAll1].[TwoFactorEnabled] AS [C10], 
        [UnionAll1].[LockoutEndDateUtc] AS [C11], 
        [UnionAll1].[LockoutEnabled] AS [C12], 
        [UnionAll1].[AccessFailedCount1] AS [C13], 
        [UnionAll1].[UserName] AS [C14], 
        [UnionAll1].[UserId] AS [C15], 
        [UnionAll1].[RoleId] AS [C16], 
        [UnionAll1].[UserId1] AS [C17], 
        [UnionAll1].[C2] AS [C18], 
        [UnionAll1].[C3] AS [C19], 
        [UnionAll1].[C4] AS [C20], 
        [UnionAll1].[C5] AS [C21], 
        [UnionAll1].[C6] AS [C22], 
        [UnionAll1].[C7] AS [C23], 
        [UnionAll1].[C8] AS [C24], 
        [UnionAll1].[C9] AS [C25]
        FROM  (SELECT 
            CASE WHEN ([Extent2].[UserId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1], 
            [Limit1].[AccessFailedCount] AS [AccessFailedCount], 
            [Limit1].[Id] AS [Id], 
            [Limit1].[Email] AS [Email], 
            [Limit1].[EmailConfirmed] AS [EmailConfirmed], 
            [Limit1].[PasswordHash] AS [PasswordHash], 
            [Limit1].[SecurityStamp] AS [SecurityStamp], 
            [Limit1].[PhoneNumber] AS [PhoneNumber], 
            [Limit1].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed], 
            [Limit1].[TwoFactorEnabled] AS [TwoFactorEnabled], 
            [Limit1].[LockoutEndDateUtc] AS [LockoutEndDateUtc], 
            [Limit1].[LockoutEnabled] AS [LockoutEnabled], 
            [Limit1].[AccessFailedCount] AS [AccessFailedCount1], 
            [Limit1].[UserName] AS [UserName], 
            [Extent2].[UserId] AS [UserId], 
            [Extent2].[RoleId] AS [RoleId], 
            [Extent2].[UserId] AS [UserId1], 
            CAST(NULL AS int) AS [C2], 
            CAST(NULL AS varchar(1)) AS [C3], 
            CAST(NULL AS varchar(1)) AS [C4], 
            CAST(NULL AS varchar(1)) AS [C5], 
            CAST(NULL AS varchar(1)) AS [C6], 
            CAST(NULL AS varchar(1)) AS [C7], 
            CAST(NULL AS varchar(1)) AS [C8], 
            CAST(NULL AS varchar(1)) AS [C9]
            FROM   (SELECT TOP (1) 
                [Extent1].[Id] AS [Id], 
                [Extent1].[Email] AS [Email], 
                [Extent1].[EmailConfirmed] AS [EmailConfirmed], 
                [Extent1].[PasswordHash] AS [PasswordHash], 
                [Extent1].[SecurityStamp] AS [SecurityStamp], 
                [Extent1].[PhoneNumber] AS [PhoneNumber], 
                [Extent1].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed], 
                [Extent1].[TwoFactorEnabled] AS [TwoFactorEnabled], 
                [Extent1].[LockoutEndDateUtc] AS [LockoutEndDateUtc], 
                [Extent1].[LockoutEnabled] AS [LockoutEnabled], 
                [Extent1].[AccessFailedCount] AS [AccessFailedCount], 
                [Extent1].[UserName] AS [UserName]
                FROM [dbo].[AspNetUsers] AS [Extent1]
                WHERE [Extent1].[Id] = @p__linq__0 ) AS [Limit1]
            LEFT OUTER JOIN [dbo].[AspNetUserRoles] AS [Extent2] ON [Limit1].[Id] = [Extent2].[UserId]
        UNION ALL
            SELECT 
            2 AS [C1], 
            [Limit2].[AccessFailedCount] AS [AccessFailedCount], 
            [Limit2].[Id] AS [Id], 
            [Limit2].[Email] AS [Email], 
            [Limit2].[EmailConfirmed] AS [EmailConfirmed], 
            [Limit2].[PasswordHash] AS [PasswordHash], 
            [Limit2].[SecurityStamp] AS [SecurityStamp], 
            [Limit2].[PhoneNumber] AS [PhoneNumber], 
            [Limit2].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed], 
            [Limit2].[TwoFactorEnabled] AS [TwoFactorEnabled], 
            [Limit2].[LockoutEndDateUtc] AS [LockoutEndDateUtc], 
            [Limit2].[LockoutEnabled] AS [LockoutEnabled], 
            [Limit2].[AccessFailedCount] AS [AccessFailedCount1], 
            [Limit2].[UserName] AS [UserName], 
            CAST(NULL AS varchar(1)) AS [C2], 
            CAST(NULL AS varchar(1)) AS [C3], 
            CAST(NULL AS varchar(1)) AS [C4], 
            [Extent4].[Id] AS [Id1], 
            [Extent4].[UserId] AS [UserId], 
            [Extent4].[ClaimType] AS [ClaimType], 
            [Extent4].[ClaimValue] AS [ClaimValue], 
            CAST(NULL AS varchar(1)) AS [C5], 
            CAST(NULL AS varchar(1)) AS [C6], 
            CAST(NULL AS varchar(1)) AS [C7], 
            CAST(NULL AS varchar(1)) AS [C8]
            FROM   (SELECT TOP (1) 
                [Extent3].[Id] AS [Id], 
                [Extent3].[Email] AS [Email], 
                [Extent3].[EmailConfirmed] AS [EmailConfirmed], 
                [Extent3].[PasswordHash] AS [PasswordHash], 
                [Extent3].[SecurityStamp] AS [SecurityStamp], 
                [Extent3].[PhoneNumber] AS [PhoneNumber], 
                [Extent3].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed], 
                [Extent3].[TwoFactorEnabled] AS [TwoFactorEnabled], 
                [Extent3].[LockoutEndDateUtc] AS [LockoutEndDateUtc], 
                [Extent3].[LockoutEnabled] AS [LockoutEnabled], 
                [Extent3].[AccessFailedCount] AS [AccessFailedCount], 
                [Extent3].[UserName] AS [UserName]
                FROM [dbo].[AspNetUsers] AS [Extent3]
                WHERE [Extent3].[Id] = @p__linq__0 ) AS [Limit2]
            INNER JOIN [dbo].[AspNetUserClaims] AS [Extent4] ON [Limit2].[Id] = [Extent4].[UserId]) AS [UnionAll1]
    UNION ALL
        SELECT 
        3 AS [C1], 
        [Limit3].[AccessFailedCount] AS [AccessFailedCount], 
        [Limit3].[Id] AS [Id], 
        [Limit3].[Email] AS [Email], 
        [Limit3].[EmailConfirmed] AS [EmailConfirmed], 
        [Limit3].[PasswordHash] AS [PasswordHash], 
        [Limit3].[SecurityStamp] AS [SecurityStamp], 
        [Limit3].[PhoneNumber] AS [PhoneNumber], 
        [Limit3].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed], 
        [Limit3].[TwoFactorEnabled] AS [TwoFactorEnabled], 
        [Limit3].[LockoutEndDateUtc] AS [LockoutEndDateUtc], 
        [Limit3].[LockoutEnabled] AS [LockoutEnabled], 
        [Limit3].[AccessFailedCount] AS [AccessFailedCount1], 
        [Limit3].[UserName] AS [UserName], 
        CAST(NULL AS varchar(1)) AS [C2], 
        CAST(NULL AS varchar(1)) AS [C3], 
        CAST(NULL AS varchar(1)) AS [C4], 
        CAST(NULL AS int) AS [C5], 
        CAST(NULL AS varchar(1)) AS [C6], 
        CAST(NULL AS varchar(1)) AS [C7], 
        CAST(NULL AS varchar(1)) AS [C8], 
        [Extent6].[LoginProvider] AS [LoginProvider], 
        [Extent6].[ProviderKey] AS [ProviderKey], 
        [Extent6].[UserId] AS [UserId], 
        [Extent6].[UserId] AS [UserId1]
        FROM   (SELECT TOP (1) 
            [Extent5].[Id] AS [Id], 
            [Extent5].[Email] AS [Email], 
            [Extent5].[EmailConfirmed] AS [EmailConfirmed], 
            [Extent5].[PasswordHash] AS [PasswordHash], 
            [Extent5].[SecurityStamp] AS [SecurityStamp], 
            [Extent5].[PhoneNumber] AS [PhoneNumber], 
            [Extent5].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed], 
            [Extent5].[TwoFactorEnabled] AS [TwoFactorEnabled], 
            [Extent5].[LockoutEndDateUtc] AS [LockoutEndDateUtc], 
            [Extent5].[LockoutEnabled] AS [LockoutEnabled], 
            [Extent5].[AccessFailedCount] AS [AccessFailedCount], 
            [Extent5].[UserName] AS [UserName]
            FROM [dbo].[AspNetUsers] AS [Extent5]
            WHERE [Extent5].[Id] = @p__linq__0 ) AS [Limit3]
        INNER JOIN [dbo].[AspNetUserLogins] AS [Extent6] ON [Limit3].[Id] = [Extent6].[UserId]) AS [UnionAll2]
    ORDER BY [UnionAll2].[C3] ASC, [UnionAll2].[C1] ASC',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'b73f9738-76ed-48d5-a8fd-cbf23a233fe9'