【发布时间】:2015-06-07 21:40:47
【问题描述】:
我有一个使用 Microsoft.Owin 3.0.1 为 CookieAuthentication 配置的 ASP.NET MVC5 应用程序。在我的应用程序中,我有一条路线,我想使用 Essential Objects EO.Pdf 库根据 Razor 视图生成 PDF。
当 PDF 生成过程开始时,会向服务器发出第二个请求,但该请求中不存在身份验证 cookie。 OWIN 拒绝该请求并重定向到登录页面。
是否可以告诉 OWIN 忽略特定路由?这是我的 Startup.cs
public partial class Startup
{
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
}
这是由此产生的异常,似乎来自 Owin:
Exception information:
Exception type: HttpException
Exception message: Server cannot append header after HTTP headers have been sent.
at System.Web.HttpHeaderCollection.SetHeader(String name, String value, Boolean replace)
at System.Web.HttpHeaderCollection.Set(String name, String value)
at Microsoft.Owin.Host.SystemWeb.CallHeaders.AspNetResponseHeaders.Set(String key, String[] values)
at Microsoft.Owin.Host.SystemWeb.CallHeaders.AspNetResponseHeaders.set_Item(String key, String[] value)
at Microsoft.Owin.Infrastructure.OwinHelpers.SetHeaderUnmodified(IDictionary`2 headers, String key, String[] values)
at Microsoft.Owin.Infrastructure.OwinHelpers.AppendHeaderUnmodified(IDictionary`2 headers, String key, String[] values)
at Microsoft.Owin.HeaderDictionary.AppendValues(String key, String[] values)
at Microsoft.Owin.Infrastructure.ChunkingCookieManager.AppendResponseCookie(IOwinContext context, String key, String value, CookieOptions options)
at Microsoft.Owin.Security.Cookies.CookieAuthenticationHandler.<ApplyResponseGrantAsync>d__f.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
【问题讨论】:
-
为什么还有第二个请求?这听起来像是真正的问题......
-
我不确定 PDF 库是如何生成 PDF 响应的,但它总是导致相同的 IIS 错误(已在问题中更新)
-
结帐msdn.microsoft.com/en-us/library/dn337263(v=vs.113).aspx。但我同意@ErikPhilips,我会找到根本问题的根源。
-
好的,谢谢,我会继续挖掘可能导致在上下文中没有经过身份验证的用户访问 OWIN 的原因。
-
好的......第二个请求是如何发出的......我确定某处有一些代码可以做到这一点?
标签: asp.net asp.net-mvc-5 owin