"Key not valid for use in specified state" Error for .Net 4.5 MVC 4 Application

20,031

I was able to fix the error by following the instructions in the following post by Vittorio Bertocci:

http://www.cloudidentity.com/blog/2013/01/28/running-wif-based-apps-in-windows-azure-web-sites-4/

Basically, I had to enable web farm cookies using the Identity and Access Tool.

In VS 2012, right click the project > select Identity Access > select the Configuration tab > check the Enable web farm ready cookies check box > click OK

Share:
20,031
ADH
Author by

ADH

Updated on July 22, 2022

Comments

  • ADH
    ADH almost 2 years

    To preface this question, please excuse me if I am getting any of my terminology wrong. The technology is very new to me.

    I have a website in MVC 4, .Net 4.5 built with VS 2012 hosted on IIS7 and have used the "Identity and Access" wizard to configure authentication using a business identity provider. I have entered a path to an STS metadata document similar to:

    https://xyz.mycompany.com/app/FederationMetadata/2007-06/FederationMetadata.xml
    

    The site is currently hosted under three different realms. The first is my local development environment, second is standard integration testing and third is development.

    http://localhost/myapp
    http://sit.mycompanytest.com/myapp
    http://dev.mycompanytest.com/myapp
    

    It is important to note that the "dev" sub-domain is in a web farm or load balanced or something. I do not currently know the exact details of the load balancing architecture.

    When I navigate to any of the above sites using IE 10 I am redirected to a login screen where I enter my credentials and gain access to the given site.

    But, on the "dev" sub-domain, when navigating around the site using links and form submissions I eventually will get the following error:

    Key not valid for use in specified state.
    

    The stack trace of the error is:

    [CryptographicException: Key not valid for use in specified state.]
    System.Security.Cryptography.ProtectedData.Unprotect(Byte[] encryptedData, Byte[] optionalEntropy, DataProtectionScope scope) +397
    System.IdentityModel.ProtectedDataCookieTransform.Decode(Byte[] encoded) +90
    
    [InvalidOperationException: ID1073: A CryptographicException occurred when attempting to decrypt the cookie using the ProtectedData API (see inner exception for details). If you are using IIS 7.5, this could be due to the loadUserProfile setting on the Application Pool being set to false. ]
    System.IdentityModel.ProtectedDataCookieTransform.Decode(Byte[] encoded) +1158198
    System.IdentityModel.Tokens.SessionSecurityTokenHandler.ApplyTransforms(Byte[] cookie, Boolean outbound) +173
    System.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver) +756
    System.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(Byte[] token, SecurityTokenResolver tokenResolver) +100
    System.IdentityModel.Services.SessionAuthenticationModule.ReadSessionTokenFromCookie(Byte[] sessionCookie) +668
    System.IdentityModel.Services.SessionAuthenticationModule.TryReadSessionTokenFromCookie(SessionSecurityToken& sessionToken) +164
    System.IdentityModel.Services.SessionAuthenticationModule.OnAuthenticateRequest(Object sender, EventArgs eventArgs) +173
    System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +165
    

    I suspect the error is happening when the load balancer changes servers.

    And, have been considering the 2nd workaround solution from the following web site: (http://blogs.msdn.com/b/distributedservices/archive/2012/10/29/wif-1-0-id1073-a-cryptographicexception-occurred-when-attempting-to-decrypt-the-cookie-using-the-protecteddata-api.aspx). But, the web site states that the solution is for .Net 4.0.

    Will that solution work for .Net 4.5? And, if not, how can I fix the error?

  • ADH
    ADH over 10 years
    Thank you for your response. I believe I have verified that both machines have the same key. I used the MMC Certificate Snap-In and they both have the same start and expiration dates. What else would I need to do to verify they are the same key? Also, each key has rights for "NETWORK SERVICE" and IIS_IUSR.