Share via


Handling HealthServiceCredentialTokenExpiredException in .NET SDK

Applications are expected to Handle the HealthServiceCredentialTokenExpiredException. The best way to handle this error is to Redirect the user to sign-on again, the best place to implement it in your basepage’s page error handler. Here is some sample code which accomplishes the same, please note this code should be in our basepage or any page deriving from HealthServicePage :

 /// <summary>
/// This error block handles the case of an expired user token. User
/// tokens expire after 24 hours or time set by persistent tokens
/// for the "keep me sign-in" feature. These tokens and are stored in the cookie,
/// which has no default timeout. Developers are encouraged to expire cookies 
/// sooner, but this code handles the case where the cookie does not
/// expire and the browser window is left open for over 24 hours.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();

    if (ex is HealthServiceCredentialTokenExpiredException)
    {
        WebApplicationUtilities.RedirectToLogOn(HttpContext.Current);
    }

    ShowError(ex);
}

Your page will need to implement ShowError method or you comment it and re-throw the un-caught exceptions.