SignInManager.PasswordSignInAsync生成许多数据库访问

Ahm*_*mel 10 sql-server asp.net asp.net-mvc sql-server-profiler asp.net-identity

在我的应用程序中,我使用ASP.NET身份.每件事都很好但通过测试,我发现以下命令产生了许多数据库访问:SignInManager.PasswordSignInAsync:

var result = await SignInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, shouldLockout: true);
Run Code Online (Sandbox Code Playgroud)

我在使用性能测试测试应用程序时发现了这个问题.测试表明登录请求需要很长时间才能获得响应.下图显示了登录请求的性能测试结果: 在此输入图像描述.

然后我使用SQL Server Profiler来检查执行命令SignInManager.PasswordSignInAsync时会发生什么.它表明生成了大量的数据库访问.以下内容取自SQL Server Profiler:

exec sp_executesql N'SELECT TOP (1) 
    [Extent1].[Id] AS [Id], 
    [Extent1].[UserFullName] AS [UserFullName], 
    [Extent1].[UserId] AS [UserId], 
    [Extent1].[Online] AS [Online], 
    [Extent1].[LastOnlineDate] AS [LastOnlineDate], 
    [Extent1].[BrithDate] AS [BrithDate], 
    [Extent1].[Job] AS [Job], 
    [Extent1].[Gender] AS [Gender], 
    [Extent1].[CountryId] AS [CountryId], 
    [Extent1].[LivesIn] AS [LivesIn], 
    [Extent1].[RelationId] AS [RelationId], 
    [Extent1].[Religion] AS [Religion], 
    [Extent1].[FirstSchool] AS [FirstSchool], 
    [Extent1].[SecondSchool] AS [SecondSchool], 
    [Extent1].[University] AS [University], 
    [Extent1].[ContactInfo] AS [ContactInfo], 
    [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 ((UPPER([Extent1].[UserName])) = (UPPER(@p__linq__0))) OR ((UPPER([Extent1].[UserName]) IS NULL) AND (UPPER(@p__linq__0) IS NULL))',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'User1@gmail.com'

exec sp_executesql N'SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[UserId] AS [UserId], 
    [Extent1].[ClaimType] AS [ClaimType], 
    [Extent1].[ClaimValue] AS [ClaimValue]
    FROM [dbo].[AspNetUserClaims] AS [Extent1]
    WHERE [Extent1].[UserId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=2103

exec sp_executesql N'SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[UserId] AS [UserId], 
    [Extent1].[ClaimType] AS [ClaimType], 
    [Extent1].[ClaimValue] AS [ClaimValue]
    FROM [dbo].[AspNetUserClaims] AS [Extent1]
    WHERE [Extent1].[UserId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=2103

exec sp_executesql N'SELECT 
    [Extent1].[LoginProvider] AS [LoginProvider], 
    [Extent1].[ProviderKey] AS [ProviderKey], 
    [Extent1].[UserId] AS [UserId]
    FROM [dbo].[AspNetUserLogins] AS [Extent1]
    WHERE [Extent1].[UserId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=2103
exec sp_executesql N'SELECT 
    [Extent1].[UserId] AS [UserId], 
    [Extent1].[RoleId] AS [RoleId]
    FROM [dbo].[AspNetUserRoles] AS [Extent1]
    WHERE [Extent1].[UserId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=2103
exec sp_executesql N'SELECT TOP (1) 
    [Extent1].[Id] AS [Id], 
    [Extent1].[UserFullName] AS [UserFullName], 
    [Extent1].[UserId] AS [UserId], 
    [Extent1].[Online] AS [Online], 
    [Extent1].[LastOnlineDate] AS [LastOnlineDate], 
    [Extent1].[BrithDate] AS [BrithDate], 
    [Extent1].[Job] AS [Job], 
    [Extent1].[Gender] AS [Gender], 
    [Extent1].[CountryId] AS [CountryId], 
    [Extent1].[LivesIn] AS [LivesIn], 
    [Extent1].[RelationId] AS [RelationId], 
    [Extent1].[Religion] AS [Religion], 
    [Extent1].[FirstSchool] AS [FirstSchool], 
    [Extent1].[SecondSchool] AS [SecondSchool], 
    [Extent1].[University] AS [University], 
    [Extent1].[ContactInfo] AS [ContactInfo], 
    [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',N'@p__linq__0 int',@p__linq__0=2103
exec sp_executesql N'SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[UserId] AS [UserId], 
    [Extent1].[ClaimType] AS [ClaimType], 
    [Extent1].[ClaimValue] AS [ClaimValue]
    FROM [dbo].[AspNetUserClaims] AS [Extent1]
    WHERE [Extent1].[UserId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=2103
exec sp_executesql N'SELECT 
    [Extent1].[LoginProvider] AS [LoginProvider], 
    [Extent1].[ProviderKey] AS [ProviderKey], 
    [Extent1].[UserId] AS [UserId]
    FROM [dbo].[AspNetUserLogins] AS [Extent1]
    WHERE [Extent1].[UserId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=2103

    exec sp_executesql N'SELECT 
        [Extent1].[UserId] AS [UserId], 
        [Extent1].[RoleId] AS [RoleId]
        FROM [dbo].[AspNetUserRoles] AS [Extent1]
        WHERE [Extent1].[UserId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=2103
    exec sp_executesql N'SELECT TOP (1) 
        [Extent1].[Id] AS [Id], 
        [Extent1].[UserFullName] AS [UserFullName], 
        [Extent1].[UserId] AS [UserId], 
        [Extent1].[Online] AS [Online], 
        [Extent1].[LastOnlineDate] AS [LastOnlineDate], 
        [Extent1].[BrithDate] AS [BrithDate], 
        [Extent1].[Job] AS [Job], 
        [Extent1].[Gender] AS [Gender], 
        [Extent1].[CountryId] AS [CountryId], 
        [Extent1].[LivesIn] AS [LivesIn], 
        [Extent1].[RelationId] AS [RelationId], 
        [Extent1].[Religion] AS [Religion], 
        [Extent1].[FirstSchool] AS [FirstSchool], 
        [Extent1].[SecondSchool] AS [SecondSchool], 
        [Extent1].[University] AS [University], 
        [Extent1].[ContactInfo] AS [ContactInfo], 
        [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',N'@p__linq__0 int',@p__linq__0=2103
    exec sp_executesql N'SELECT 
        [Extent1].[Id] AS [Id], 
        [Extent1].[UserId] AS [UserId], 
        [Extent1].[ClaimType] AS [ClaimType], 
        [Extent1].[ClaimValue] AS [ClaimValue]
        FROM [dbo].[AspNetUserClaims] AS [Extent1]
        WHERE [Extent1].[UserId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=2103
exec sp_executesql N'SELECT 
    [Extent1].[LoginProvider] AS [LoginProvider], 
    [Extent1].[ProviderKey] AS [ProviderKey], 
    [Extent1].[UserId] AS [UserId]
    FROM [dbo].[AspNetUserLogins] AS [Extent1]
    WHERE [Extent1].[UserId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=2103
exec sp_executesql N'SELECT 
    [Extent1].[UserId] AS [UserId], 
    [Extent1].[RoleId] AS [RoleId]
    FROM [dbo].[AspNetUserRoles] AS [Extent1]
    WHERE [Extent1].[UserId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=2103
exec sp_executesql N'SELECT TOP (1) 
    [Extent1].[Id] AS [Id], 
    [Extent1].[UserFullName] AS [UserFullName], 
    [Extent1].[UserId] AS [UserId], 
    [Extent1].[Online] AS [Online], 
    [Extent1].[LastOnlineDate] AS [LastOnlineDate], 
    [Extent1].[BrithDate] AS [BrithDate], 
    [Extent1].[Job] AS [Job], 
    [Extent1].[Gender] AS [Gender], 
    [Extent1].[CountryId] AS [CountryId], 
    [Extent1].[LivesIn] AS [LivesIn], 
    [Extent1].[RelationId] AS [RelationId], 
    [Extent1].[Religion] AS [Religion], 
    [Extent1].[FirstSchool] AS [FirstSchool], 
    [Extent1].[SecondSchool] AS [SecondSchool], 
    [Extent1].[University] AS [University], 
    [Extent1].[ContactInfo] AS [ContactInfo], 
    [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',N'@p__linq__0 int',@p__linq__0=2103
exec sp_executesql N'SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[UserId] AS [UserId], 
    [Extent1].[ClaimType] AS [ClaimType], 
    [Extent1].[ClaimValue] AS [ClaimValue]
    FROM [dbo].[AspNetUserClaims] AS [Extent1]
    WHERE [Extent1].[UserId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=2103
Run Code Online (Sandbox Code Playgroud)

以上查询只是结果的一部分.多次执行相同的查询.这是正常情况还是有问题?

如果这是一个问题,它是如何解决的.

scg*_*ugh 2

我假设您正在使用“现有的”身份设置。在这种情况下,您也可能会使用实体框架。

回答您的问题:根据我的经验,执行操作时UserManager,位于 等后面的 UserStore会被多次访问。SignInManager

我注意到的主要问题是,使用 EntityFramework 时速度非常慢。我发现,如果我创建自己的 CustomUserStore,它会显着加快登录/用户操作速度。仍然有对这些方法的多次调用,但性能要好得多。

就我而言,我使用 Micro-ORMDapper.NET来驱动我的新 UserStore。Dapper 执行查询的速度比 EntityFramework 快得多(根据某些测试中的查询,快 3-10 倍 - 请参阅:https ://www.exceptionnotfound.net/dapper-vs-entity-framework-vs-ado-net-performance-基准测试/

Dapper.NET:https://github.com/StackExchange/Dapper

通过自定义 UserStore 重写,您可以在线找到大量示例,但这里是一个: https: //markjohnson.io/articles/exorcising-entity-framework-from-asp-net-identity/