在使用ASP.NET Core干净架构中的ApplicationUser、IdentityUser和UserManager时出现问题。

6

我正在开发一个基于 ASP.NET Core 6.0 WebAPI 的干净架构。https://github.com/jasontaylordev/CleanArchitecture

这个架构中有 4 个项目:WebApi、Infrastructure、Application 和 Domain。

  • Domain 是核心。
  • Application 层依赖于 Domain 层。
  • Infrastructure 层依赖于 Application 层。
  • WebApi 层依赖于 Application 层和 Infrastructure 层。

而查询和命令(CQRS)应该写在 Application 层内。

我想使用 Asp.Net Core Identity。

ApplicationUser.cs(位于 Infrastructure/Identity 目录下)

namespace Infrastructure.Identity;

public class ApplicationUser : IdentityUser
{
  // removed
}

IIdentityService.cs(位于应用程序/公共/接口中)

namespace Application.Common.Interfaces;

public interface IIdentityService
{
    Task<string> GetUserNameAsync(string userId);

    Task<bool> IsInRoleAsync(string userId, string role);

    Task<bool> AuthorizeAsync(string userId, string policyName);

    Task<(Result Result, string UserId)> CreateUserAsync(string userName, string password);

    Task<Result> DeleteUserAsync(string userId);

   // and so on
}

IdentityService.cs(位于 Infrastructure/Identity 中)

namespace Infrastructure.Identity;

public class IdentityService : IIdentityService
{
    private readonly UserManager<ApplicationUser> _userManager;

    public IdentityService(
        UserManager<ApplicationUser> userManager )
    {
        _userManager = userManager;
    }

  //implementations of Interface by using userManager
}

所有工作都很好。

我的问题是,我必须在应用程序层中使用UserManager<ApplicationUser> userManager来编写查询。

我收到错误消息:不必要的using指令。[Application] 命名空间“Infrastructure”中不存在类型或命名空间。您是否缺少程序集引用?)[Application]

Query.cs(位于Application / SiteCodes / Queries / GetAll中)

using Infrastructure.Identity;   // error

namespace Application.SiteCodes.Queries.GetAll
{
    public class GetAllQueryHandler : IRequest<List<SiteCode>>
    {

    }
    public class GetAllQueryHandlerHandler : IRequestHandler<GetAllQueryHandler,List<SiteCode>>
    {
        private readonly UserManager<ApplicationUser> _userManager;  // error
        private readonly IHttpContextAccessor _httpContextAccessor;

       // constructor removed 

       public async Task<List<SiteCode>> Handle(GetAllQueryHandler request, 
           CancellationToken cancellationToken)
        {
            var user = await 
           _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User);   // By using usermanger. I have to call more function like this from useManager

           // removed rest
        }
    }
}

如何在应用层调用userManager和ApplicationUser(:IdentyUser)而不出错?

请帮帮我。

注意:我不想自己编写类似useManager包的函数。我想使用userManager。

例如:像这样

private readonly UserManager<ApplicationUser> _userManager;

_userManager.GetUserAsync(_httpContextAccessor.HttpContext.User);

有人有解决这个问题的想法吗?

1个回答

5
在应用程序层中,您只应使用公共接口。例如,在此处,您不应直接使用UserManager类,而应使用公共接口IIdentityService。
应用程序层被描述为:
此层定义外部层实现的接口。例如,如果应用程序需要访问通知服务,则会在应用程序中添加新接口,并在基础设施中创建实现。
这意味着您可以根据需要扩展IIdentityService接口,并在基础设施层中进行相应的实现。
您还不应在应用程序层中使用IHttpContextAccessor,因为它是WebUI层的一部分。
解决问题的一些想法:
-IHttpContextAccessor应仅在WebUI层中访问:在API控制器中,从其中获取当前用户,然后从UserManager检索ApplicationUser及其ID。 -将用户ID传递到应用程序层 -使用该用户ID使用IIdentityService

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接