Wednesday, March 30, 2016

System.Security.Cryptography.CryptographicException: Error occurred during a cryptographic operation.

Problem:
I have an MVC 5 project using ADFS on prem as user control.  The application works fine but at some point after running for a few minutes I start getting the Crypto error.  The only left to do to recover is to kill all IE sessions and restart the web app.  This is still in staging so not a prod problem yet.




Solution:
This is an issue with the machine key.  If you do not specify it in configuration then it gets created with each new version of the app you publish.  The work around is to catch the error.  This can be done in the Global.asax.  The application Error event traps the error.  Once this happens we force a signout and clear the error.  The app will then force the a reauthentication "automagically".  This should prevent the user from seeing the failure.


// Be sure to reference System.IdentityModel.Services
// and include using System.IdentityModel.Services;
// at the start of your class
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();
    }
}






Source
  1. http://stackoverflow.com/questions/25857577/error-occurred-during-a-cryptographic-operation-when-decrypting-forms-cookie
  2. http://stackoverflow.com/questions/14119965/federated-authentication-on-azure

Tuesday, March 29, 2016

MVC 5 bootstrap template not running under IIS express 7

Problem:

I have been running MVC project using default bootstrap template.  It works fine when I deploy it to our staging server.  When I attempt to run it locally on my desktop none of the bootstrap files are working.  I get no branding.


I wracked my brain on this and thought it might be missing files.  Then hit f12 on the browser and saw several of these gems:


Error:
Failed to load resource: the server https://localhost/Scripts/Content/bootstrap.css responded with a status of 401 (Unauthorized)

Issue:

Appears that the css and js files are being blocked in iis express.  I am running this site using SSL and ADFS trust.  This appears to be causing some issues with local file access.  I know that IIS express runs as the local user.  I still granted IUSR and IIS_USRS accounts full access to the folders where the site was running on my local box.  This appears to have resolved the issue.




Source:

http://stackoverflow.com/questions/25973749/failed-to-load-resource-the-server-responded-with-a-status-of-401-unauthorized
http://stackoverflow.com/questions/5388698/mvc-i-have-deployed-my-application-but-css-only-works-when-i-log-in

Monday, March 28, 2016

Get specific version of project from TFS 2012


































Select project from TFS Collection






















View History


Select changeset and version history icon
















      

Test if Guid variable is null

Another test for Guid variable.  .Net Framework will initialize variable when it is declared.
Test for the default condition.


if (test.guid == Guid.Empty)
{
//your condition
}

How to test if DateTime is null

Pretty straightforward you need to remember that the .Net stack gives any time date variable a min value by default.  You just need to test for the presence of that min value.


if(test.lastupdate == DateTime.MinValue)
{
//your condition
}

Monday, March 21, 2016

Web config transform on microsoft.identityModel - 'http://schemas.microsoft.com/XML-Document-Transform' attribute is not declared

Problem:

Have my claims aware MVC app humming right along.  Now I wanted to get it to deploy easily between dev and staging.  I tried to use Web.config transform but could not get the Microsoft.IdentityModel elements to transform.  Appears that the namespace (
xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform) and visual studio 2013 don't play nice together.


Solution:

Fortunately, I am not the first to run into this issue.  Fix is easy


<audienceUris>

<add xdt:Transform="RemoveAll"/>

<add value=https://realm/mine xdt:Transform="Insert"/>

</audienceUris>




Drop the existing key in the audienceUris, Removeall.  Then add in the value that you need using the Insert.


Source:

  1. http://stackoverflow.com/questions/8513488/web-config-transform-on-microsoft-identitymodel-http-schemas-microsoft-com
  2. http://blogs.catapultsystems.com/rswitzer/archive/2013/08/05/web-config-transforms-for-identity-model/

Friday, March 18, 2016

wif10201: No valid key mapping found for securityToken: 'System.IdentityModel.Tokens.X509SecurityToken' and issuer: 'http://login.eur.army.mil/adfs/services/trust'.

There are might be 2 cases for this error.
  1. Get the actual thumbprint from ADFS and put in web.config thumbprint tag
  2. mismatch in port number (running application port number and port number which is configured in ADFS)
First option was my issue.  I changed the thumbprint and the issue was resolved.


Source:
http://stackoverflow.com/questions/24304097/no-valid-key-mapping-found-for-securitytoken