aws / aws-aspnet-cognito-identity-provider

ASP.NET Core Identity Provider for Amazon Cognito
https://aws.amazon.com/developer/language/net/
Apache License 2.0
215 stars 89 forks source link

TypeLoadException, Method 'NormalizeName' in type 'CognitoKeyNormalizer' does not have an implementation #129

Closed steingran closed 4 years ago

steingran commented 5 years ago

Scenario: Trying to use AWS Cognito as the Identity provider in a Blazor Server .NET Core 3.0 project, following these guidelines: https://aws.amazon.com/blogs/developer/now-generally-available-the-asp-net-core-identity-provider-for-amazon-cognito/

However, when running I get this exception:

System.TypeLoadException HResult=0x80131522 Message=Method 'NormalizeName' in type 'Amazon.AspNetCore.Identity.Cognito.CognitoKeyNormalizer' from assembly 'Amazon.AspNetCore.Identity.Cognito, Version=1.0.2.0, Culture=neutral, PublicKeyToken=885c28607f98e604' does not have an implementation. Source=Amazon.AspNetCore.Identity.Cognito StackTrace: at Microsoft.Extensions.DependencyInjection.CognitoServiceCollectionExtensions.InjectCognitoUser[TUser](IServiceCollection services, Action1 identityOptions) at Microsoft.Extensions.DependencyInjection.CognitoServiceCollectionExtensions.AddCognitoIdentity(IServiceCollection services, Action1 identityOptions, String prefix) at Company.Product.Startup.ConfigureServices(IServiceCollection services) in C:\GitHub\Product\src\Web\Product\Startup.cs:line 45 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) in //src/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs:line 469 at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services) in //src/Hosting/Hosting/src/Internal/ConfigureServicesBuilder.cs:line 50 at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>cDisplayClass9_0.gStartup|0(IServiceCollection serviceCollection) in //src/Hosting/Hosting/src/Internal/ConfigureServicesBuilder.cs:line 28 at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services) in //src/Hosting/Hosting/src/Internal/ConfigureServicesBuilder.cs:line 26 at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>cDisplayClass8_0.b0(IServiceCollection services) in //src/Hosting/Hosting/src/Internal/ConfigureServicesBuilder.cs:line 22 at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services) in //src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs:line 236 at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.b_0(HostBuilderContext context, IServiceCollection services) in //src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs:line 209 at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() in //src/Hosting/Hosting/src/HostBuilder.cs:line 221 at Microsoft.Extensions.Hosting.HostBuilder.Build() in //src/Hosting/Hosting/src/HostBuilder.cs:line 133 at Company.Product.Program.Main(String[] args) in C:\GitHub\Product\src\Web\Product\Program.cs:line 18

Seems like a method is missing an implementation?

steingran commented 5 years ago

And my Target framework is like this in my csproj file:

netcoreapp3.0

I see in https://github.com/aws/aws-aspnet-cognito-identity-provider/blob/6f8d985deef49bcce485d27cd52acf8ab9d45aaa/src/Amazon.AspNetCore.Identity.Cognito/CognitoKeyNormalizer.cs that there is an "#if NETCOREAPP_3_0", and NETCOREAPP_3_0 is defined like this in https://github.com/aws/aws-aspnet-cognito-identity-provider/blob/192ef661cab94135264a155625d36ad43878d3a8/src/Amazon.AspNetCore.Identity.Cognito/Amazon.AspNetCore.Identity.Cognito.csproj:

<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
  <DefineConstants>NETCOREAPP_3_0</DefineConstants>

So in theory, this should work just fine. But it doesn't, for some reason. Does anyone have a clue as to why this happens?

klaytaybai commented 5 years ago

Hi @steingran, you need to use version 1.0.3.x instead of 1.0.2.x.

klaytaybai commented 5 years ago

Sorry. It looks like this hasn't been published to NuGet yet. I'll try to find out why.

assyadh commented 5 years ago

Relates https://github.com/aws/aws-aspnet-cognito-identity-provider/issues/103

steingran commented 5 years ago

@klaytaybai Yeah, I use 1.0.2 and that is the newest version on Nuget as of now. Great that a new version is coming with a fix for this 👍

jjamid commented 5 years ago

This library is really great. 1.0.3 fixes the issue for me too. However, I did get a general "object reference not set" exception when using SendEmailConfirmationTokenAsync during registration. Did I use it wrong ?

 var result = await _userManager.CreateAsync(user, Input.Password);        
if (result.Succeeded)
{
    _logger.LogInformation("User created a new account with password.");
     await _userManager.SendEmailConfirmationTokenAsync(user);    <---

In addition, is there any way of making this work with the default identity implementation (that includes UI) without having to override each and every razor view ?

something like:

services.AddDefaultIdentity<CognitoUser>()
        .AddSignInManager<SignInManager<CognitoUser>>
        .Add...

for example the default Logout razor needs only changing the type of the injected SignInManager, and I'm sure many other pages do not change their implementation.

@page
@using Microsoft.AspNetCore.Identity
@attribute [IgnoreAntiforgeryToken]
@inject SignInManager<IdentityUser> SignInManager      <----
@functions {
    public async Task<IActionResult> OnPost()
    {
        if (SignInManager.IsSignedIn(User))
        {
            await SignInManager.SignOutAsync();
        }

        return Redirect("~/");
    }
}

It seems that the main issue is that CognitoUser does not inherit IdentityUser ?

ChelseaKR commented 5 years ago

Thanks @klaytaybai! Has there been any luck in getting this to NuGet?

TehGM commented 5 years ago

Seems that for now if I want to proceed with the project, I need to compile 1.0.3 myself. That's good that there's a working version at all, but really hope we can get it on nuget soon (been over 2 weeks).

atul898 commented 5 years ago

Any updates on when Amazon.AspNetCore.Identity.Cognito, Version=1.0.3 is coming out on nuget?

TNadir commented 5 years ago

Any updates on when Amazon.AspNetCore.Identity.Cognito, Version=1.0.3 is coming out on nuget?

joined to question

assyadh commented 4 years ago

The team is actively working on updating our internal tooling to support .NET Core 3.0. The package will be on NuGet shortly. Thanks

assyadh commented 4 years ago

https://www.nuget.org/packages/Amazon.AspNetCore.Identity.Cognito/1.0.3 is now out and supports 3.0