ASP.Net Identity正在对每个请求执行以下查询。我没有更改创建MVC项目时生成的任何身份代码。我的ax
代码在下方。
请注意,Startup.Auth.cs
设置为validateInterval
。另请注意,Cookie过期时已明确设置(TimeSpan.FromMinutes(30)
)设置为ExpireTimeSpan
。我找到了a similar question on SO,其回答表明TimeSpan.FromMinutes(30)
应足以阻止ASP.Net Identity在每次请求时都与DB联系。
我在这里缺少什么?
Startup.Auth.cs
TimeSpan.FromMinutes(30)
对每个请求执行查询(标准ASP.Net标识查询)
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext < ApplicationUserManager > (ApplicationUserManager.Create);
app.CreatePerOwinContext < ApplicationSignInManager > (ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator
.OnValidateIdentity < ApplicationUserManager, ApplicationUser > (
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),
}
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(30)
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
// Enables the application to remember the second login verification factor such as phone or email.
// Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
// This is similar to the RememberMe option when you log in.
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
}
修改
我做了做了一件我忘了提及的改变。我们将与身份相关的表移动到SQL DB。以下变更:
exec sp_executesql N'SELECT
[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] = @p0',N'@p0 nvarchar(4000)',@p0=N'[ID]'
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 nvarchar(4000)',@p__linq__0=N'[ID]'
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 nvarchar(4000)',@p__linq__0=N'[ID]'
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 nvarchar(4000)',@p__linq__0=N'[ID]'
答案 0 :(得分:1)
好吧,这个让我觉得有点傻。在单步执行代码并注意到regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
被正确调用而不是重复调用之后,我做了一些进一步的挖掘。
在导航菜单局部视图中,我找到了这行代码:
<a href="#" class="nav-link dropdown-toggle">@("@" +
ApplicationUser.CurrentUser.Email.Split('@')[0])</a>
在每个页面加载上执行该代码导致上面的4个查询被运行。 headdesk