domaindrivendev / Swashbuckle.WebApi

Seamlessly adds a swagger to WebApi projects!
BSD 3-Clause "New" or "Revised" License
3.07k stars 677 forks source link

Runtime errors when asp.net identity kicks in #327

Open mattwoberts opened 9 years ago

mattwoberts commented 9 years ago

Hi,

I'm struggling with ASP.NET Identity - it's breaking the swagger UI.

When I deploy my web app, with the web api and swashbuckle to my hosting provider (which is an Azure website), everything works for a few mins, then I start getting this error (below). I can see that the stack includes a mention of Identity, so in looking into that I've found that the issue is thus:

In my ASP.NET Identity setup, I configure this typical code to tell Identity to check the supplied cookie against the security stamp (to make sure I don't need to log in again)

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, User, int>(
            validateInterval: TimeSpan.FromMinutes(1), 
            regenerateIdentityCallback: (manager, user) =>
            {
                return user.GenerateUserIdentityAsync(manager);
            },
            getUserIdCallback: id => (int.Parse(id.GetUserId()))
            ),
    },
 });

Now, when the code executes, if you're in the swagger UI (/swagger/ui/index), then when it tries to regenerate your identity, that's when you get this error. Here's the error:

Error message
System.Web.HttpException: Server cannot append header after HTTP headers have been sent.

Stack trace
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)
    ... chopped .. 
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Identity.Owin.IdentityFactoryMiddleware`2.<Invoke>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow()
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar)
at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar)
at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Any ideas about tackling this?

NicoloBeltrame commented 8 years ago

Same issue!

ador-mg commented 8 years ago

Same here! Any solution for this?