IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Identityserver reboot - antiforgery token failed to deserialize though id token is valid user needs to login again

Open jmatheti opened this issue 9 years ago • 2 comments

  • [x] I read and understood how to enable logging

Issue / Steps to reproduce the problem

on web server restart, the access token from client to resource is working but identity token is not being recognised. connect/authorized endpoint throwing an exception "System.Security.Cryptography.CryptographicException: The key was not found in the key ring"

Deploy Idmserver, Js Application and web api resource to IIS login in to client JS app and make a call to api Restart IIS (Manager web site)

try to login again, you will see a login page, though token is not expired neither cookie.

Identity server configuration

services.AddIdentityServer(options =>
           {
               options.Events.RaiseSuccessEvents = true;
               options.Events.RaiseFailureEvents = true;
               options.Events.RaiseErrorEvents = true;
               //options.Authentication.CookieLifetime = TimeSpan.FromSeconds(30);
               options.Authentication.CookieLifetime = TimeSpan.FromMinutes(20);
           }).AddSigningCredential(cert)
                //.AddInMemoryIdentityResources(Config.GetIdentityResources())
                //.AddInMemoryApiResources(Config.GetApiResources())
                //.AddInMemoryClients(Config.GetClients())
                //.AddTestUsers(Config.GetUsers());
                .AddConfigurationStore(builder =>
                    builder.UseSqlServer(connectionString, options =>
                        options.MigrationsAssembly(migrationsAssembly)))
                .AddOperationalStore(builder =>
                    builder.UseSqlServer(connectionString, options =>
                         options.MigrationsAssembly(migrationsAssembly)))
                .AddAspNetIdentity<ApplicationUser>()                
                .AddProfileService<ProfileService>();

Relevant parts of the log file

2017-09-06 15:41:17.696 +10:00 [Debug] Mapping found for endpoint: Authorize, creating handler: "IdentityServer4.Endpoints.AuthorizeEndpoint"
2017-09-06 15:41:17.701 +10:00 [Information] Invoking IdentityServer endpoint: "IdentityServer4.Endpoints.AuthorizeEndpoint" for "/connect/authorize"
2017-09-06 15:41:17.706 +10:00 [Debug] Start authorize request
2017-09-06 15:41:17.720 +10:00 [Debug] No user present in authorize request
2017-09-06 15:41:17.723 +10:00 [Debug] Start authorize request protocol validation
2017-09-06 15:41:17.988 +10:00 [Debug] "JsClient1" found in database: True
2017-09-06 15:41:18.017 +10:00 [Debug] Found ["openid", "profile"] identity scopes in database
2017-09-06 15:41:18.041 +10:00 [Debug] Found ["testapi1"] API scopes in database
2017-09-06 15:41:18.052 +10:00 [Debug] Found ["openid", "profile"] identity scopes in database
2017-09-06 15:41:18.054 +10:00 [Debug] Found ["testapi1"] API scopes in database
2017-09-06 15:41:18.060 +10:00 [Debug] Calling into custom validator: "IdentityServer4.Validation.DefaultCustomAuthorizeRequestValidator"
2017-09-06 15:41:18.093 +10:00 [Information] ValidatedAuthorizeRequest
"{
  \"ClientId\": \"JsClient1\",
  \"ClientName\": \"JavaScript Client1\",
  \"RedirectUri\": \"http://localhost:9001/callback.html\",
  \"AllowedRedirectUris\": [
    \"http://localhost:9001/callback.html\"
  ],
  \"SubjectId\": \"anonymous\",
  \"ResponseType\": \"id_token token\",
  \"ResponseMode\": \"fragment\",
  \"GrantType\": \"implicit\",
  \"RequestedScopes\": \"openid profile testapi1\",
  \"State\": \"94fe6ff1a8ea4f5195823060a96bd3b1\",
  \"Nonce\": \"446f748dec274f1190b4538880499cf0\",
  \"Raw\": {
    \"client_id\": \"JsClient1\",
    \"redirect_uri\": \"http://localhost:9001/callback.html\",
    \"response_type\": \"id_token token\",
    \"scope\": \"openid profile testapi1\",
    \"state\": \"94fe6ff1a8ea4f5195823060a96bd3b1\",
    \"nonce\": \"446f748dec274f1190b4538880499cf0\"
  }
}"
2017-09-06 15:41:18.098 +10:00 [Information] Showing login: User is not authenticated
2017-09-06 15:41:18.382 +10:00 [Debug] Start authorize request protocol validation
2017-09-06 15:41:18.387 +10:00 [Debug] "JsClient1" found in database: True
2017-09-06 15:41:18.387 +10:00 [Debug] Found ["openid", "profile"] identity scopes in database
2017-09-06 15:41:18.389 +10:00 [Debug] Found ["testapi1"] API scopes in database
2017-09-06 15:41:18.389 +10:00 [Debug] Found ["openid", "profile"] identity scopes in database
2017-09-06 15:41:18.391 +10:00 [Debug] Found ["testapi1"] API scopes in database
2017-09-06 15:41:18.391 +10:00 [Debug] Calling into custom validator: "IdentityServer4.Validation.DefaultCustomAuthorizeRequestValidator"
2017-09-06 15:41:18.397 +10:00 [Debug] "JsClient1" found in database: True
2017-09-06 15:41:18.397 +10:00 [Debug] "" found in database: False
2017-09-06 15:41:19.902 +10:00 [Error] An exception was thrown while deserializing the token.
System.InvalidOperationException: The antiforgery token could not be decrypted. ---> System.Security.Cryptography.CryptographicException: The key {d2a274f8-2780-4e85-b171-748a7d0e5508} was not found in the key ring.
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean& requiresMigration, Boolean& wasRevoked)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)
   at Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)
   at Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery.GetCookieTokenDoesNotThrow(HttpContext httpContext)

jmatheti avatar Sep 06 '17 06:09 jmatheti

This is not related to idm...

but the solution that worked is /``` /REFERENCE https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-storage-providers //REFERENCE persists keys http://www.tugberkugurlu.com/archive/asp-net-core-authentication-in-a-load-balanced-environment-with-haproxy-and-redis services.AddDataProtection() //Microsoft.AspNetCore.DataProtection.Redis package .PersistKeysToFileSystem(new DirectoryInfo("F:\Jana\Certs\Keys\"));

jmatheti avatar Sep 07 '17 00:09 jmatheti

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 14 '20 08:01 lock[bot]