Unable to cast object of type System.Security.Claims.ClaimsPrincipal to type Microsoft.IdentityModel.Claims.IClaimsPrincipal Unable to cast object of type System.Security.Claims.ClaimsPrincipal to type Microsoft.IdentityModel.Claims.IClaimsPrincipal azure azure

Unable to cast object of type System.Security.Claims.ClaimsPrincipal to type Microsoft.IdentityModel.Claims.IClaimsPrincipal


What you observe is result of mixing .NET 3.5 WIF (Microsoft.IdentityModel) and WIF 4.0 (System.IdentityModel & System.Security).What I suggest is:

  1. Remove reference to Microsoft.IdentityModel.* assemblies in your project
  2. Add reference to System.IdentityModel & System.IdentityModel.Services assemblies
  3. Fix using statements
  4. Fix references to Microsoft.IdentityModel in your Web.Config

Do a backup copy of your project before doing this, because, if you haven't done this before, you might end up with a lot of error and not working code. But the main idea is that you have to get rid of all and any Microsoft.IdentityModel references and you will be good.


I am able resolved this issue as per astaykov suggestion and i changed the code like below,

using System.Security.Claims;var identity = User.Identity as ClaimsIdentity;                 foreach (var claim in identity.Claims)        {            if (claim.Type.Contains("EmailAddress"))            {                ViewBag.EmailName = claim.Value;            }        }