57228
.net core 2.1を使用しています。

```

    using Microsoft.AspNetCore.Identity;
    
    private readonly UserManager<string> _userManager;
    public ManagementController(UserManager<string> userManager) 
    {
         _userManager = userManager; 
    }
    
```

をコントローラーのクラス内に書いて実行したところ、以下の例外がページにアクセスした時点で発生しました。

```

    System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[System.Object]' while attempting to activate '(略).Controllers.ManagementController'.
       at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
       at lambda_method(Closure , IServiceProvider , Object[] )
       at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.<CreateActivator>b__0(ControllerContext controllerContext)
       at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
       at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
       at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
       at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
       at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
    
```

Identityの認証自体は成功したのですが、UserManagerの適切な使い方が分かりません。

例外の原因、またUserManagerの型には何を指定するのが適切なのかを教えていただければ幸いです。
CC BY-SA 4.0
2019-08-03T11:41:36.883
57145
2019-08-07T17:07:44.970
29331
question
c# mvc asp.net-core
asp.net core identityでUserManagerを初期化できない
おそらく、これらのソースが該当するのだと思われます。  
`UserManager<string>`ではなく、`UserManager<ApplicationUser>`を使うのでしょう。

[AspNetCore/src/MusicStore/test/MusicStore.Test/ManageControllerTest.cs](https://github.com/aspnet/AspNetCore/blob/173cf1786ec0ca0bd5b227e1b299efb9f2241f2c/src/MusicStore/test/MusicStore.Test/ManageControllerTest.cs)

>
```

>     var userManager =
> _fixture.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
>  
```

[AspNetCore/src/Identity/samples/IdentitySample.Mvc/Controllers/ManageController.cs](https://github.com/aspnet/AspNetCore/blob/c95ee2b051814b787b07f55ff224d03d550aafeb/src/Identity/samples/IdentitySample.Mvc/Controllers/ManageController.cs)

>
```

>     private readonly UserManager<ApplicationUser> _userManager;
>  
```

[AspNetCore/src/MusicStore/samples/MusicStore/Controllers/ManageController.cs](https://github.com/aspnet/AspNetCore/blob/37399bc2175cc253e83bd2d7b2a66e613daad38a/src/MusicStore/samples/MusicStore/Controllers/ManageController.cs)

>
```

>     public UserManager<ApplicationUser> UserManager { get; }
>  
```

* * *

そして、その元となる`<ApplicationUser>`は、自分で定義するのだと思われます。

[AspNetCore/src/Identity/samples/IdentitySample.DefaultUI/Data/ApplicationUser.cs](https://github.com/aspnet/AspNetCore/blob/c95ee2b051814b787b07f55ff224d03d550aafeb/src/Identity/samples/IdentitySample.DefaultUI/Data/ApplicationUser.cs)

>
```

>     public class ApplicationUser : IdentityUser
>     {
>         [ProtectedPersonalData]
>         public string Name { get; set; }
>         [PersonalData]
>         public int Age { get; set; }
>     }
>  
```

[ASP.NET Core 2.1 Identity couldn't find
ApplicationUser](https://stackoverflow.com/q/52015990/9014308)

> You'll have to create the ApplicationUser class yourself. The default user
> is IdentityUser. ApplicationUser inherits from IdentityUser:
```

>     public class ApplicationUser : IdentityUser
>     {
>     }
>  
```

[カスタム ユーザー データ](https://docs.microsoft.com/ja-
jp/aspnet/core/security/authentication/customize-identity-
model?view=aspnetcore-2.2#custom-user-data)

> [カスタム ユーザー データ](https://docs.microsoft.com/ja-
> jp/aspnet/core/security/authentication/add-user-
> data?view=aspnetcore-2.2&tabs=visual-
> studio)から継承することではサポートされて`IdentityUser`します。 この型名前を指定するが通例  
>  `ApplicationUser`:
```

>     public class ApplicationUser : IdentityUser
>     {
>         public string CustomTag { get; set; }
>     }
>  
```

まあ元の[IdetifyUser Class](https://docs.microsoft.com/en-
us/dotnet/api/microsoft.aspnetcore.identity.entityframeworkcore.identityuser?view=aspnetcore-1.1&viewFallbackFrom=aspnetcore-2.2)
に既にあるプロパティで問題ないならば、追加の情報は必要なく、空で定義するだけなのでしょう。

[AspNetCore/src/Identity/ApiAuthorization.IdentityServer/samples/ApiAuthSample/Models/ApplicationUser.cs](https://github.com/aspnet/AspNetCore/blob/bfec2c14be1e65f7dd361a43950d4c848ad0cd35/src/Identity/ApiAuthorization.IdentityServer/samples/ApiAuthSample/Models/ApplicationUser.cs)

>
```

>     // Add profile data for application users by adding properties to the
> ApplicationUser class
>     public class ApplicationUser : IdentityUser
>     {
>     }
>  
```

* * *

同時に他にも意識するところがあるようですが。(引用は省略)

[Unable to get IdentityUser data from ASP.NET Core 2.1
controller](https://stackoverflow.com/q/53824206/9014308)  
[AspNetCore/src/Identity/samples/IdentitySample.DefaultUI/Data/ApplicationDbContext.cs](https://github.com/aspnet/AspNetCore/blob/c95ee2b051814b787b07f55ff224d03d550aafeb/src/Identity/samples/IdentitySample.DefaultUI/Data/ApplicationDbContext.cs)  
[AspNetCore/src/MusicStore/samples/MusicStore/Models/MusicStoreContext.cs](https://github.com/aspnet/AspNetCore/blob/87a92e52c8b4bb7cb75ff78d53d641b1d34f8775/src/MusicStore/samples/MusicStore/Models/MusicStoreContext.cs)

* * *

あとこんなページもあるようなので、色々と変わってきているのかもしれませんが。

[ASP.NET Core プロジェクトにおけるIdentityのスキャフォールディング](https://docs.microsoft.com/ja-
jp/aspnet/core/security/authentication/scaffold-
identity?view=aspnetcore-2.2&tabs=visual-studio)  
[ASP.NET Core プロジェクトにおける Identity へのカスタム
ユーザーデータの追加、ダウンロードおよび削除](https://docs.microsoft.com/ja-
jp/aspnet/core/security/authentication/add-user-
data?view=aspnetcore-2.2&tabs=visual-studio)  
[ASP.NET Core での Identity モデルのカスタマイズ](https://docs.microsoft.com/ja-
jp/aspnet/core/security/authentication/customize-identity-
model?view=aspnetcore-2.2)
CC BY-SA 4.0
2019-08-07T16:16:25.027
57228
2019-08-07T17:07:44.970
2019-08-07T17:07:44.970
26370
26370
57145
answer