忽略 ASP.NET 身份中静态文件的身份验证
Ignore Authentication Validation for static files in ASP.NET Identity
我在我的应用程序中使用 asp.net 身份 2 来实现身份系统。
当我使用 EF Profiler 分析我的应用程序时。
我发现它在每个静态文件请求中连接到数据库时存在一些问题。这对性能和页面加载速度非常不利。
为了解决这个问题我写了下面的代码:
Global.asax
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (shouldIgnoreRequest())
return;
if (Context.User == null)
return;
}
private bool shouldIgnoreRequest()
{
string[] reservedPath =
{
"/__browserLink",
"/favicon.ico",
"/img",
"/css"
,"/w",
"/js"
};
var rawUrl = Context.Request.RawUrl;
if (reservedPath.Any(path => rawUrl.StartsWith(path, StringComparison.OrdinalIgnoreCase)))
{
return true;
}
return BundleTable.Bundles.Select(bundle => bundle.Path.TrimStart('~'))
.Any(bundlePath => rawUrl.StartsWith(bundlePath, StringComparison.OrdinalIgnoreCase));
}
RouteConfig.cs
routes.IgnoreRoute("img/{*pathinfo}");
routes.IgnoreRoute("js/{*pathinfo}");
routes.IgnoreRoute("css/{*pathinfo}");
routes.IgnoreRoute("{file}.gif");
routes.IgnoreRoute("{file}.jpg");
routes.IgnoreRoute("{file}.js");
routes.IgnoreRoute("{file}.css");
routes.IgnoreRoute("{file}.png");
routes.IgnoreRoute("{file}.pdf");
routes.IgnoreRoute("{file}.htm");
routes.IgnoreRoute("{file}.html");
routes.IgnoreRoute("{file}.swf");
routes.IgnoreRoute("{file}.txt");
routes.IgnoreRoute("{file}.xml");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
如何忽略此身份验证请求?
尝试自定义 OnValidateIdentity 以忽略不需要的请求:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
// ...
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = context =>
{
if(shouldIgnoreRequest(context)) // How to ignore Authentication Validations for static files in ASP.NET Identity
{
return Task.FromResult(0);
}
return container.GetInstance<IApplicationUserManager>().OnValidateIdentity().Invoke(context);
}
},
// ...
});
使用这个方法
private static bool shouldIgnoreRequest(CookieValidateIdentityContext context)
{
string[] reservedPath =
{
"/__browserLink",
"/img",
"/fonts",
"/Scripts",
"/Content",
"/Uploads",
"/Images"
};
return reservedPath.Any(path => context.OwinContext.Request.Path.Value.StartsWith(path, StringComparison.OrdinalIgnoreCase)) ||
BundleTable.Bundles.Select(bundle => bundle.Path.TrimStart('~')).Any(bundlePath => context.OwinContext.Request.Path.Value.StartsWith(bundlePath,StringComparison.OrdinalIgnoreCase));
}
我在我的应用程序中使用 asp.net 身份 2 来实现身份系统。 当我使用 EF Profiler 分析我的应用程序时。
我发现它在每个静态文件请求中连接到数据库时存在一些问题。这对性能和页面加载速度非常不利。
为了解决这个问题我写了下面的代码:
Global.asax
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (shouldIgnoreRequest())
return;
if (Context.User == null)
return;
}
private bool shouldIgnoreRequest()
{
string[] reservedPath =
{
"/__browserLink",
"/favicon.ico",
"/img",
"/css"
,"/w",
"/js"
};
var rawUrl = Context.Request.RawUrl;
if (reservedPath.Any(path => rawUrl.StartsWith(path, StringComparison.OrdinalIgnoreCase)))
{
return true;
}
return BundleTable.Bundles.Select(bundle => bundle.Path.TrimStart('~'))
.Any(bundlePath => rawUrl.StartsWith(bundlePath, StringComparison.OrdinalIgnoreCase));
}
RouteConfig.cs
routes.IgnoreRoute("img/{*pathinfo}");
routes.IgnoreRoute("js/{*pathinfo}");
routes.IgnoreRoute("css/{*pathinfo}");
routes.IgnoreRoute("{file}.gif");
routes.IgnoreRoute("{file}.jpg");
routes.IgnoreRoute("{file}.js");
routes.IgnoreRoute("{file}.css");
routes.IgnoreRoute("{file}.png");
routes.IgnoreRoute("{file}.pdf");
routes.IgnoreRoute("{file}.htm");
routes.IgnoreRoute("{file}.html");
routes.IgnoreRoute("{file}.swf");
routes.IgnoreRoute("{file}.txt");
routes.IgnoreRoute("{file}.xml");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
如何忽略此身份验证请求?
尝试自定义 OnValidateIdentity 以忽略不需要的请求:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
// ...
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = context =>
{
if(shouldIgnoreRequest(context)) // How to ignore Authentication Validations for static files in ASP.NET Identity
{
return Task.FromResult(0);
}
return container.GetInstance<IApplicationUserManager>().OnValidateIdentity().Invoke(context);
}
},
// ...
});
使用这个方法
private static bool shouldIgnoreRequest(CookieValidateIdentityContext context)
{
string[] reservedPath =
{
"/__browserLink",
"/img",
"/fonts",
"/Scripts",
"/Content",
"/Uploads",
"/Images"
};
return reservedPath.Any(path => context.OwinContext.Request.Path.Value.StartsWith(path, StringComparison.OrdinalIgnoreCase)) ||
BundleTable.Bundles.Select(bundle => bundle.Path.TrimStart('~')).Any(bundlePath => context.OwinContext.Request.Path.Value.StartsWith(bundlePath,StringComparison.OrdinalIgnoreCase));
}