Bug 22282 - Mono 3.6.0 ClaimsPrincipal and ClaimsIdentity constructors fail and/or do not match .net 4.5
Summary: Mono 3.6.0 ClaimsPrincipal and ClaimsIdentity constructors fail and/or do not...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-08-22 06:48 UTC by Robert van der Boon
Modified: 2014-08-25 07:12 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Unit tests and NUnit output (5.98 KB, application/x-zip-compressed)
2014-08-22 06:48 UTC, Robert van der Boon
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Robert van der Boon 2014-08-22 06:48:32 UTC
Created attachment 7766 [details]
Unit tests and NUnit output

While trying to use the ClaimsIdentity and ClaimsPrincipal in my project using Mono 3.6.0 I encountered a lot of problems in the constructor behaviour for both ClaimsIdentity and ClaimsPrincipal. This prevents me from using the authn and authz parts of the Microsoft.Owin/ASP.NET WebAPI stack.

These errors can be categorized as:
- constructor missing, but is available on .Net 4.5 (thus throw a missing method exception, found this one while creating unit tests on windows, are running the resulting assembly on mono):
  ClaimsIdentity..ctor(IEnumerable<Claims>)
- internal errors (like NullRef exception, ArgumentNullException)
- functional erros
  - specified AuthenticationType override is not used (i.e. taken from base identity instead of using the specified override)
  - ClaimsPrincipal ctor(IIdentity) does not use the identity
  - values not set as specified in the documentation and/or as .Net 4.5 does

As work has been done in mono-master in this area I've also tested against mono-master (93e590439ca940b89b9871a8392a5248838c806c), which at least fixes the missing method exceptions, but still isn't right.

I've attached my unit tests for ClaimsPrincipal and ClaimsIdentity, see ClaimsIdentityTest.cs and ClaimsPrincipalTest.cs (which all succeed on .Net 4.5).

The mono output of the unittests is attached as corlib_claims_mono-3.6.0.txt and corlib_claims_mono-master_20140822.txt

How to reproduce:
- build the unittests on Microsoft.Net (building on mono would fail due to missing methods and/or due to the fact that GenericIdentity/GenericPrincipal do not derive from ClaimsIdentity/ClaimsPrincipal on Mono)
   csc /r:nunit.framework.dll /define:NET_4_5 /debug /o- /out:corlib-tests-claims-constructor.dll /target:library ClaimsIdentityTest.cs ClaimsPrincipalTest.cs
- run the unittests using Mono.
   mono nunit-console.exe -nologo -framework=4.5 corlib-tests.dll -labels

Expected behaviour:
- The constructors for mono 3.6.x and newer do not throw exceptions
- The constructors for mono 3.6.x and newer behave like .Net 4.5
- GenericIdentity and GenericPrincipal derive from ClaimsIdentity/Principal (on the net_4_5 profile)
Comment 1 Marek Safar 2014-08-22 16:09:47 UTC
Fixed in master.

Thanks for the contribution.
Comment 2 Robert van der Boon 2014-08-25 02:54:56 UTC
Thanks, that was amazingly quick.

Two minor things looking at the commit (don't know if this is the right place to mention it):
1. GenericPrincipal does not inherit from ClaimsPrincipal, but it does in .net 4.5 (not a big deal for me, but I thought I'd mention it for completeness)

2. ClaimsIdentity.HasClaim(type,value) now does a OrdinalCaseInsensitive match on both type and value. Reading MSDN (and creating a small test) I'd have thought it should be OrdinalCaseInsensitive on Type and Ordinal on Value.

      [Test]
      public void HasClaim_typeValue_Works()
      {
         var id = new ClaimsIdentity(
            new[] {
               new Claim ("claim_type", "claim_value"),
               new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 
               new Claim ("claim_role_type", "claim_role_value"), 
            }, "test_authority");

         Assert.IsTrue(id.HasClaim("claim_type", "claim_value"), "#1");
         Assert.IsTrue(id.HasClaim("cLaIm_TyPe", "claim_value"), "#2");
         Assert.IsFalse(id.HasClaim("claim_type", "cLaIm_VaLuE"), "#3");
         Assert.IsFalse(id.HasClaim("Xclaim_type", "claim_value"), "#4");
         Assert.IsFalse(id.HasClaim("claim_type", "Xclaim_value"), "#5");
      }
Comment 3 Marek Safar 2014-08-25 07:12:58 UTC
Fixed in master as well.