Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am developing one MVC4 application which authenticate corp domain users using ADFS and i have written the code and i am getting the error like below, am i not getting claims ?

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

    public ActionResult Index()
    {
        try
        {

            IClaimsPrincipal principal = (IClaimsPrincipal)Thread.CurrentPrincipal;
            IClaimsIdentity identity = (IClaimsIdentity)principal.Identity;
            if (String.IsNullOrEmpty(identity.Claims[0].Value.Split('@')[0]))
            {
                ViewData["Message"] = string.Format("You are email is :{0}", identity.Claims[0].Value.Split('@')[0]);

            }
            else
            {
                ViewData["Message"] = "You are not getting any claims";
            }
        }
        catch (Exception ex)
        {
            ViewData["Message"] = "Something wrong.";
        }
        return View();
    }
like image 559
user3240560 Avatar asked Mar 10 '14 15:03

user3240560


People also ask

What is a ClaimsPrincipal?

A claims principal has a collection of ClaimsIdentity objects that is accessible through the Identities property. Each ClaimsIdentity in the collection contains one or more claims. The Claims property returns all of the claims from all of the claims identities in this collection.

What is ClaimsPrincipal in .NET core?

The Claims property returns all of the claims contained by the identities associated with the principal. In the uncommon case in which the ClaimsPrincipal contains multiple ClaimsIdentity instances, you can use the Identities property or you can access the primary identity by using the Identity property.

What is ClaimsIdentity in MVC?

ClaimsIdentity(IIdentity, IEnumerable<Claim>, String, String, String) Initializes a new instance of the ClaimsIdentity class from the specified IIdentity using the specified claims, authentication type, name claim type, and role claim type.

What is FindFirst C#?

FindFirst(Predicate<Claim>) Retrieves the first claim that is matched by the specified predicate. FindFirst(String) Retrieves the first claim with the specified claim type.


2 Answers

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.

like image 200
astaykov Avatar answered Nov 09 '22 04:11

astaykov


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;
            }
        }
like image 40
user3240560 Avatar answered Nov 09 '22 03:11

user3240560