How do you use IPrincipal and IIdentity in the portable class libraries?
Asked Answered
I

1

6

With WIF (Windows Identity Foundation) 4.5, Microsoft created the WindowsPrincipal class, which is a type of ClaimsPrincipal. Of course, these classes aren't portable, but the interfaces behind them are (IPrincipal). The same can be said of the ClaimsIndentity class implementing the IIdentity interface.

The problem I have is that these classes, and WIF in general is based entirely on the concept of "claims", which is awesome... but the two interfaces, IPrincipal and IIdentity are not. Not only that, but the ClaimsPrincipal class also has a collection of Identities instead of just a single Identity associated to it.

  • IPrincipal has Identity and IsInRole members.
  • IIdentity has AuthenticationType, IsAuthenticated, and Name members.

Given the fact that the Portable Class Libraries can only access these two interfaces, how does one go about getting the actual claims?

Also, in the rare instance that a principal has multiple identities, how does one get the "non-primary" identities?

Input answered 21/5, 2014 at 18:43 Comment(1)
As you have said that every ClaimsPrincipal have multiple identities and every identity have collection of claims so you can access any identity by getting into the reference variable of ClaimsPrincipal. Like ClaimsPrincipal.Identities[1] for the second non primary identityParticular
R
4

Microsoft provided claims aware types in Microsoft.IdentityModel.dll which is not portable (yet, I hope). Those types just extends current identity types e.g. IPrincipal:

public interface IClaimsPrincipal : IPrincipal

It means that claims aware types are compatibile with old code which uses IPrincipal and IIdentity interfaces. But to make your code claims aware you must add a reference to Microsoft.IdentityModel.dll (which is not available as a PCL) or write it from scratch.

If you want to test how old code behaves when processing instances of claims aware types, you can just use downcasting to the IPrincipal interface:

IClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(new List<IClaimsIdentity>()
{
    new ClaimsIdentity("AuthType1"),
    new ClaimsIdentity("AuthType2")
});

IPrincipal principal = claimsPrincipal as IPrincipal;
IIdentity identity = principal.Identity;
Redford answered 19/6, 2014 at 7:51 Comment(1)
FYI, The namespace Microsoft.IdentityModel isn't WIF 4.5. WIF 4.5 was included in .NET 4.5 so all the proper namespaces start with System. e.g. System.IdentityModel, System.Security.Claims. There are migration guides if you look around.Lycaonia

© 2022 - 2024 — McMap. All rights reserved.