ASP.NET Identity问题:Application_EndRequest中RedirectLocation为null
Hey there, let's break down why you're seeing RedirectLocation as null in Application_EndRequest and how to fix it. I've worked through similar issues with ASP.NET Identity's authentication pipeline, so here are actionable steps to resolve this:
1. Understand When Application_EndRequest Runs
First, a key point: Application_EndRequest fires after the response has been sent to the client. By this stage, ASP.NET may already have cleaned up or reset the RedirectLocation property, especially if the redirect was handled by the authentication middleware (like CookieAuthentication).
If you need to capture the redirect URL, it's better to hook into earlier pipeline stages rather than waiting for EndRequest.
2. Capture Redirects in Authentication Middleware's OnApplyRedirect
The default CookieAuthenticationMiddleware handles most identity-related redirects (like login/logout). You can intercept the redirect URL here and store it for later use in Application_EndRequest:
Modify your ConfigureAuth method in Startup.Auth.cs to add the OnApplyRedirect handler:
public void ConfigureAuth(IAppBuilder app) { app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), // Add this handler to capture redirect URLs OnApplyRedirect = context => { // Store the redirect URI in OwinContext for later access context.OwinContext.Set<string>("StoredRedirectLocation", context.RedirectUri); // Execute the default redirect logic context.Response.Redirect(context.RedirectUri); } }); // Rest of your auth configuration... }
Then, in Global.asax.cs's Application_EndRequest, retrieve the stored value:
protected void Application_EndRequest() { var owinContext = HttpContext.Current.GetOwinContext(); var redirectLocation = owinContext.Get<string>("StoredRedirectLocation"); // Now you can use redirectLocation (it won't be null if a redirect was triggered by the auth middleware) }
3. Verify if a Redirect Actually Occurred
RedirectLocation is only populated when the response has a 3xx status code (301, 302, etc.). Before panicking about a null value, check the response status code first:
protected void Application_EndRequest() { var response = HttpContext.Current.Response; if (response.StatusCode >= 300 && response.StatusCode < 400) { var redirectLocation = response.RedirectLocation; // Handle the redirect URL here } else { // No redirect happened, so null is expected } }
4. Check for Custom Modules/Filters Modifying the Response
If you have custom HTTP modules, action filters, or middleware that alters the response, they might be clearing RedirectLocation or canceling the redirect. Try temporarily disabling any custom code that touches the response pipeline to see if the issue goes away. This helps isolate whether the problem comes from your custom logic or the default Identity setup.
内容的提问来源于stack exchange,提问作者Alisson Reinaldo Silva




