简体   繁体   中英

How to handle CryptographicException after long inactivity in ASP .NET MVC with Azure AD credentials?

I'm using Azure Active Directory application which contains user data and credentials. I access ASP .NET MVC 5 application with this credentials using Microsoft.Azure.ActiveDirectory.GraphClient library. After long inactivity, about 20 minutes, and clicking some link I got plain Server Runtime Error and cannot refresh page and login again. Only clearing cookies works.

What I tried:

  • handle this error with customErrors - doesn't seem to work
  • catching it in:

protected void Application_Error(object sender, EventArgs e) { var error = Server.GetLastError(); var cryptoEx = error as CryptographicException; if (cryptoEx != null) { FederatedAuthentication.WSFederationAuthenticationModule.SignOut(); Server.ClearError(); HttpContext.Current.Response.Redirect("~/"); } }

as described here: Federated Authentication on Azure

  • problem does not exist while I'm using debugger and local machine, only on production - does it depend on IIS config?
  • may I change sessionTimeout of Azure AD cookies not to wait 20 minutes every time?
  • should I use 'static' machine key - not generated every time by Azure AD?

我终于做了一个解决方法:我没有处理这个令人讨厌的异常,而是只是通过JavaScript调度的请求来保持会话,这些请求正在调用控制器并设置了一些Session["Value"]

I also handled this by making sure my pages refresh every 30 minutes.

This can be done in HTML by adding the meta tag

<meta http-equiv="refresh" content="1800">

It can also be done via Javascript

setInterval(function() {
              window.location.reload();
            }, 180000); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM