Microsoft.AspNetCore.Identity 弃用版本问题

问题描述 投票:0回答:1

由于 Microsoft.AspNetCore.Identity 已弃用,我尝试安装 Microsoft.AspNetCore.Identity.EntityFrameworkCore 但在应用程序层中使用 SignInManager 时遇到问题。这是

.csproj
文件的确切代码。我可以看到 UserManager,因为 Identity.EntityFrameworkCore 包使用 Microsoft.Extensions.Identity.Core 包,但在此扩展包中没有 SignInManager。

实际代码:

using Microsoft.AspNetCore.Identity;

private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager;

public UserManagementService(UserManager<User> userManager, SignInManager<User> signInManager)
{
    _userManager = userManager;
    _signInManager = signInManager;
}

.csproj 文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Mapster" Version="6.5.1" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.28" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.28" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Forum.Domain\Forum.Domain.csproj" />
    <ProjectReference Include="..\Forum.Persistence\Forum.Persistence.csproj" />
  </ItemGroup>
</Project>

我该怎么做才能实现这个功能。

我尝试将此功能移至 API 层,并且成功了。 API 中没有 Asp.NetCore.Identity 这样的东西,但不知何故它使用了该包的 6.0.0 版本,该版本在包管理器中不可用。

c# asp.net-core asp.net-core-webapi
1个回答
0
投票

UserManager 存在是因为它针对 netstandard。 Signinmanager 与 asp.net、cookies、http 等紧密耦合,因此不适合类库。因此,由于设计原因,您的类库需要同时面向 .NET Core 6.0,并且需要对 Microsoft.AspNetCore.App 的 FrameworkReference:

 <FrameworkReference Include="Microsoft.AspNetCore.App" />
。以下是您可以用作参考的示例:

添加 Microsoft.AspNetCore.App 包之前:

添加后:

<Project Sdk="Microsoft.NET.Sdk">

       <PropertyGroup>
             <TargetFramework>net6.0</TargetFramework>
             <ImplicitUsings>enable</ImplicitUsings>
             <Nullable>enable</Nullable>
       </PropertyGroup>

       <ItemGroup>
             <FrameworkReference Include="Microsoft.AspNetCore.App" />
             <PackageReference Include="Mapster" Version="6.5.1" />
             <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.28" />
             <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.28" />
       </ItemGroup>

       
</Project>

© www.soinside.com 2019 - 2024. All rights reserved.