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.
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);
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.
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
//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.
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
I am very glad to help you find the root cause of the problem on this thread .
Shadow_Kittencorn
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
19 Points
45 Posts
Error using custom claims transformer (System.Linq)
Apr 17, 2019 05:14 PM|Shadow_Kittencorn|LINK
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?
Contributor
2070 Points
606 Posts
Re: Error using custom claims transformer (System.Linq)
Apr 18, 2019 08:13 AM|Sherry Chen|LINK
Hi Shadow_Kittencorn ,
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
19 Points
45 Posts
Re: Error using custom claims transformer (System.Linq)
Apr 18, 2019 08:21 AM|Shadow_Kittencorn|LINK
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.
Contributor
2070 Points
606 Posts
Re: Error using custom claims transformer (System.Linq)
Apr 18, 2019 09:44 AM|Sherry Chen|LINK
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
19 Points
45 Posts
Re: Error using custom claims transformer (System.Linq)
Apr 18, 2019 09:52 AM|Shadow_Kittencorn|LINK
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)!
Contributor
2070 Points
606 Posts
Re: Error using custom claims transformer (System.Linq)
May 03, 2019 02:14 AM|Sherry Chen|LINK
Hi Shadow_Kittencorn ,
I am very glad to help you find the root cause of the problem on this thread .
In general, this is not possible, but the specific issue need to be tested and analyzed according to the specific project.
Best Regards ,
Sherry
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.