Jam*_*ill 8 c# asp.net-mvc asp.net-identity
ASP.Net Identity正在对每个请求执行以下查询.我没有更改创建MVC项目时生成的任何身份代码.我的Startup.Auth.cs代码如下.
请注意,validateInterval设置为TimeSpan.FromMinutes(30).另请注意,Cookie expiration正在显式设置(ExpireTimeSpan)设置为TimeSpan.FromMinutes(30).我在SO上发现了一个类似的问题,其回答表明TimeSpan.FromMinutes(30)应该足以阻止ASP.Net Identity在每次请求时联系DB.
我在这里错过了什么?
Startup.Auth.cs
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);
}
}
Run Code Online (Sandbox Code Playgroud)
正在对每个请求执行的查询(标准ASP.Net标识查询)
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]'
Run Code Online (Sandbox Code Playgroud)
编辑
我确实做了一个我忘记提及的改变.我们将与身份相关的表移动到SQL DB.更改如下:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
// Changed connection string name
public ApplicationDbContext()
: base("MaestroConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,这让我感觉有点愚蠢。在单步执行代码并注意到它regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))被正确调用并且没有重复调用之后,我做了一些进一步的挖掘。
在导航菜单部分视图中,我发现了这行代码:
<a href="#" class="nav-link dropdown-toggle">@("@" +
ApplicationUser.CurrentUser.Email.Split('@')[0])</a>
Run Code Online (Sandbox Code Playgroud)
在每次页面加载时执行该代码都会导致运行上述 4 个查询。总台。
| 归档时间: |
|
| 查看次数: |
330 次 |
| 最近记录: |