title: keycloak doesn't work with asp.net MVC5 web app (C#)
body: <p>I am trying to connect my MVC5 web application with Keycloak server v1.98. It is connected. When I access my web app, Keycloak require entering the credentials, and when I enter it I get the exception below:</p>
  
  <p>My Configuration (startup.cs):</p>
  
  <pre><code>public void Configuration(IAppBuilder app)
      {
          const string persistentAuthType = "WebApplication1_cookie_auth";
  
          // --- Cookie Authentication Middleware - Persists user sessions between requests
          app.UseCookieAuthentication(new CookieAuthenticationOptions
          {
              AuthenticationType = persistentAuthType
          });
          app.SetDefaultSignInAsAuthenticationType(persistentAuthType); // Cookie is primary session store
  
          // --- Keycloak Authentication Middleware - Connects to central Keycloak database
          app.UseKeycloakAuthentication(new KeycloakAuthenticationOptions
          {
              // App-Specific Settings
              ClientId = "dotnettest", // *Required*
  
              VirtualDirectory = "", // Set this if you use a virtual directory when deploying to IIS
  
              // Instance-Specific Settings
              Realm = "dotnettest", // Don't change this unless told to do so
              KeycloakUrl = "http://127.0.0.1:9090/auth", // Enter your Keycloak URL here
  
              // Template-Specific Settings
              SignInAsAuthenticationType = persistentAuthType, // Sets the above cookie with the Keycloak data
              AuthenticationType = "WebApplication1_keycloak_auth", // Unique identifier for the auth middleware
              ClientSecret = "187a2ba7-91f9-479f-a290-2b249a64236a"
          });
      }
  </code></pre>
  
  <p>Exception Details: </p>
  
  <pre><code>System.Exception: Both the access token and the refresh token have expired
  </code></pre>
  
  <p>Stack Trace: </p>
  
  <pre><code>[Exception: Both the access token and the refresh token have expired]
     KeycloakIdentityModel.&lt;GetClaimsAsync&gt;d__39.MoveNext() +708
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
     KeycloakIdentityModel.&lt;ToClaimsIdentityAsync&gt;d__25.MoveNext() +156
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
     System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) +11522180
     Owin.Security.Keycloak.Middleware.&lt;InvokeAsync&gt;d__1.MoveNext() +1066
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
     Microsoft.Owin.Security.Infrastructure.&lt;Invoke&gt;d__0.MoveNext() +445
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
     Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.&lt;RunApp&gt;d__5.MoveNext() +187
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
     Microsoft.Owin.Security.Infrastructure.&lt;Invoke&gt;d__0.MoveNext() +653
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
     Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.&lt;RunApp&gt;d__5.MoveNext() +187
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
     Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.&lt;DoFinalWork&gt;d__2.MoveNext() +185
     Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +69
     Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +64
     System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +380
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155
  </code></pre>
  
  <p>I followed this tutorial: </p>
  
  <p><a href="https://github.com/dylanplecki/KeycloakOwinAuthentication/wiki/ASP.NET-MVC-Tutorial" rel="nofollow noreferrer">https://github.com/dylanplecki/KeycloakOwinAuthentication/wiki/ASP.NET-MVC-Tutorial</a></p>
  
  <p>Thanks.</p>