I have implemented the Azure AD B2c in my .net core MVC Application. Where after logged in to my application if i click the browser back button, it redirects to an error page
Loading
I have implemented the Azure AD B2c in my .net core MVC Application. Where after logged in to my application if i click the browser back button, it redirects to an error page
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Abhishek YadavPosted Aug 23, 2023, 5:19 AM
To resolve this issue, you can follow these steps:
Add the
[NoCache]attribute to the action methods of your controllers.Implement the
[NoCache]attribute using a custom filter attribute or the response headers.Example:
Configure the Startup.cs file to add the necessary headers:
ConfigureServicesmethod, add the following code:Configuremethod, add the following code:By following these steps, you should be able to prevent the redirect to the error page when clicking the browser back button after logging in.
Cr BhargaviPosted Aug 23, 2023, 4:06 AM
Please ensure that you have appropriate cache control headers set in your application's responses, especially on the pages involved in the authentication process and please make sure you're handling token expiration properly in your application. You might need to refresh the token or re-authenticate if it has expired.
Mohammad HussainPosted Aug 21, 2023, 3:38 PM
Experiencing issues with Azure AD B2C and browser navigation after logging in is not uncommon. This can often be due to the caching behavior of browsers or how Azure AD B2C handles the session.
Here are some steps you can take to address this issue:
1. Page Caching: Ensure that your application's pages are not being cached by the browser. You can do this by setting appropriate cache-control headers in your responses. This will prevent the browser from showing cached pages when navigating back.
In ASP.NET Core, you can set cache control in the `Startup.cs` file's `ConfigureServices` method using the `AddHttpCacheHeaders` method:
2. Session Management: Azure AD B2C sessions can be managed using policies and session timeout settings. Make sure you have the appropriate session timeout configured to prevent unexpected behavior when navigating back.
3. Logout and Redirect: Implement proper logout mechanisms in your application. When a user logs out, you should redirect them to Azure AD B2C's logout endpoint. This helps in clearing the session and tokens. You can implement a controller action to initiate the logout and redirect:
Ensure that you are using the correct authentication scheme (in this example, it's `CookieAuthenticationDefaults.AuthenticationScheme`).
4. Post-Logout Redirect URI: In your Azure AD B2C application settings, ensure that you have configured the correct "Post-Logout Redirect URI". This is the URL where users will be redirected after logging out from Azure AD B2C.
5. Prompt Behavior: You can also control the behavior of Azure AD B2C when users navigate back to your application after logging in. Use the `prompt` parameter in your login requests to control whether the user should be prompted for credentials again or not. For example:
Remember that the behavior might vary based on the browser and the specific configuration of your application and Azure AD B2C. It's recommended to thoroughly test your application's behavior after implementing any changes.