locked
Error using custom claims transformer (System.Linq) RRS feed

  • Question

  • User1352447851 posted

    I had a VS 2017 ASP.NET Core 2.2 Project and I wanted to start a new project based on the same code.

    I created a new project and copied in the main code. I then reinstalled the NuGet packages (same versions) and checked that all the configs were the same. I renamed the namespace to be the same as the project.

    In startup.cs:

    services.AddAuthentication(IISDefaults.AuthenticationScheme);

    services.AddAuthorization(options =>
    {
    options.AddPolicy("GroupOne", policy =>
    policy.Requirements.Add(new GroupRequirement(new string [] { Environment.GetEnvironmentVariable("GROUP")})));

    });

    services.AddTransient<IClaimsTransformation, CustomClaimsTransformer>();

    Transformer - if I set a breakpoint in here it never gets hit:

    using MyProject.MyManagement;
    using Microsoft.AspNetCore.Authentication;
    using System.Linq;
    using System.Security.Claims;
    using System.Threading.Tasks;

    namespace MyProject.Test
    {
    public class CustomClaimsTransformer : IClaimsTransformation
    {
    public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
    {
    var identity = principal.Identities.FirstOrDefault(x => x.IsAuthenticated);
    if (identity == null) return Task.FromResult(principal);

    var user = identity.Name;
    if (user == null) return Task.FromResult(principal);


    ((ClaimsIdentity)principal.Identity).AddClaim(new Claim("Code", TestManagement.GetCode(user)));
    return Task.FromResult(principal);
    }
    }
    }

    The only error I get on the webpage is that the claim doesn't exist.

    VS Output: Exception thrown: 'System.InvalidOperationException' in System.Linq.dll

    Chrome:Failed to load resource: the server responded with a status of 500 (Internal Server Error)

    Setting a breakpoint in the claims transformer doesn't seem to do anything.

    Renaming the namespace in the old project works fine, so the main difference is that I manually reinstalled the dependences using NuGet. I have gone through every config and property menu and they seem identical. 

    I have no idea how to get better errors?

    Wednesday, April 17, 2019 5:14 PM

Answers

  • User-854763662 posted

    Hi Shadow_Kittencorn ,

    1. Check if you test the project in the Debug environment. If you test in the release environment , the breakpoint will not get hit anyway.

    2. Check if app.UseAuthentication(); is in Startup.cs ,and the order of it must be before the app.UseMVC();

    3. The thrown error should show the location of it , which class , which line . You could find and check the detailed error

    4. Here is my test project , could you share the sample demo of your project that can reproduce the issue ?

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2019 9:44 AM

All replies

  • User-854763662 posted

    Hi Shadow_Kittencorn ,

    Renaming the namespace in the old project works fine, so the main difference is that I manually reinstalled the dependences using NuGet. I have gone through every config and property menu and they seem identical. 

    I test the code you provided on ASP.NET Core 2.2 Project  ,but there is no error thrown . 

    What is the NuGet packages you manually installed?

    The breakpoint in the CustomClaimsTransformer method should get hit when the user log in ,and use step into(F11) to check where the error  occurs .

    Best Regards ,

    Sherry

    Thursday, April 18, 2019 8:13 AM
  • User1352447851 posted

    Sherry Chen

    The breakpoint in the CustomClaimsTransformer method should get hit when the user log in ,and use step into(F11) to check where the error  occurs .

    Edit:

    Run Order:

    app.UseMvc();

     services.Configure<CookiePolicyOptions>(options =>

                {

                    // This lambda determines whether user consent for non-essential cookies is needed for a given request.

                    options.CheckConsentNeeded = context => true;

                    options.MinimumSameSitePolicy = SameSiteMode.None;

                });

    _Layout.cshtml:

    @User.Claims.First(c => c.Type == "Code").Value

    //Crash Exception thrown: 'System.InvalidOperationException' in System.Linq.dll - (Claim Code) does not exist - which I can verify in the debugger. Unfortunately it works just fine in my old project.

    I have narrowed it down to being thrown directly after running the above. I dont understand why my claims transformer just isn't being run.

    The NuGet packages I have installed are:

    BuildBulderMinifier

    Microsoft.AspNetCore.App

    Microsoft.AspNetCore.Razor.Design

    Microsoft.AspNetCore.Razor.Lanuage

    Microsoft.Extensions.DependancyInjection

    Microsoft.Extensions.DependancyInjectionAbstractions

    Microsoft.VisualStudio.Web.CodeGeneration.Design

    Microsoft.Windows.Compatibility

    NLog

    NLog.Extensions.Logging

    NLog.Targets.Syslog

    NLog.Web.AspNetCore

    System.DirectoryServices

    System.DirectoryServices.AccountManagement

    I may not require all of these, but I was learning as building the code. 

    Thursday, April 18, 2019 8:21 AM
  • User-854763662 posted

    Hi Shadow_Kittencorn ,

    1. Check if you test the project in the Debug environment. If you test in the release environment , the breakpoint will not get hit anyway.

    2. Check if app.UseAuthentication(); is in Startup.cs ,and the order of it must be before the app.UseMVC();

    3. The thrown error should show the location of it , which class , which line . You could find and check the detailed error

    4. Here is my test project , could you share the sample demo of your project that can reproduce the issue ?

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2019 9:44 AM
  • User1352447851 posted

    Check if app.UseAuthentication(); is in Startup.cs ,and the order of it must be before the app.UseMVC();

    Thanks - this was the issue. I don't really understand how my old code is running just fine without it (in both Dev and Production)!

    Thursday, April 18, 2019 9:52 AM
  • User-854763662 posted

    Hi Shadow_Kittencorn ,

    I am very glad to help you find the root cause of the problem on this thread .

    I don't really understand how my old code is running just fine without it (in both Dev and Production)!

    In general, this is not possible, but the specific issue need to be tested and analyzed according to the specific project.

    Best Regards ,

    Sherry

    Friday, May 3, 2019 2:14 AM