How do you use IPrincipal and IIdentity in portable class libraries?

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

The problem is that these classes and WIF as a whole are completely based on the "claims" concept, which is amazing ... but the two IPrincipal and IIdentity interfaces are not. And not only that, but the ClaimsPrincipal class also contains a set of identities, and not just one identifier associated with it.

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

Given the fact that portable class libraries can only access these two interfaces, how can the actual requirements be obtained?

In addition, in the rare case when a leader has several identities, how to get "non-primary" identities?

+6
source share
1 answer

Microsoft has provided complaint-supported ad types in Microsoft.IdentityModel.dll that are not portable (but I hope). These types only extend current types of identity, for example. IPrincipal :

 public interface IClaimsPrincipal : IPrincipal 

This means that claims-supporting types are compatible with old code that uses the IPrincipal and IIdentity . But in order for your code requirements to be known, you must add a link to Microsoft.IdentityModel.dll (which is not available as PCL) or write it from scratch.

If you want to check how the old code works when handling instances of types that support claims, you can simply use downcasting for 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; 
+4
source

Source: https://habr.com/ru/post/969656/


All Articles