简体   繁体   English

如何使用用户管理器功能制作网络核心库

[英]How to make a net core library with usermanager functions

Once again i stumbled across a problem.我又一次偶然发现了一个问题。 Well its not realy a problem, its more a thing that i want to learn.好吧,它不是真正的问题,它更像是我想学习的东西。 I want to start learning class libraries for .net core.我想开始学习 .net 核心的 class 库。 First of i created a class library project with the right settings.首先,我创建了一个具有正确设置的 class 库项目。 Now i want to create functions which use for example现在我想创建例如使用的函数

applicationusermanager应用程序用户管理器

I have no idea how to access the applicationusermanager from my other project.我不知道如何从我的其他项目访问 applicationusermanager。 While i was reading some posts i saw that it has something to do with dependencies.当我阅读一些帖子时,我发现它与依赖关系有关。 Once again, i dont have a clue if its even possible or how should start.再一次,我不知道它是否可能或应该如何开始。

Lets for example say that i have a library function like this:例如,假设我有一个库 function,如下所示:

public static async Task<ApplicationUser> Get_User_Test()
    {
        return await _usermanager.FindByEmailAsync("test@test.com");
    }

I want this function to run in my main project.我希望这个 function 在我的主项目中运行。 I call this by doing library.Get_User_Test();我通过做library.Get_User_Test();来调用它but it fails.但它失败了。

Some good articles or suggestions would be appreciated.一些好的文章或建议将不胜感激。 I just simply dont know where to start.我只是不知道从哪里开始。

I hope you are all doing good and are healthy.我希望你们都做得很好,身体健康。 Thanks for reading this and i whish you a good day.感谢您阅读本文,祝您有美好的一天。

Kind regards,亲切的问候,

Nick缺口

UserManager provides the APIs for managing user in a persistence store . UserManager provides the APIs for managing user in a persistence store It is a bare bone of ASP.NET Core Identity to provide all authentication/authorization logic when you work with .NET Core.当您使用 .NET 核心时,它是ASP.NET Core Identity的一个基本框架,可提供所有身份验证/授权逻辑。 It is a cross-cutting logic so you don't want to test itself.这是一个横切的逻辑,所以你不想测试自己。 The only thing you perform Unit Test is your custom IUserStore when you choose to integrate Database without Entity Framework Core here .当您在此处选择在没有Entity Framework Core的情况下集成数据库时,您执行单元测试的唯一事情是您的自定义IUserStore

You can imagine UserManager will call each implemented method in your implemented IUserStore class.您可以想象UserManager将调用您实现的IUserStore class 中的每个实现方法。 Because it is your custom code, so you should write UT for this.因为它是您的自定义代码,所以您应该为此编写 UT。

There is a sample code.有一个示例代码。

public class MongoUserStore : IUserStore<ApplicationUser>
{
    private readonly IUserRepository _userRepo;
    public MongoUserStore(IUserRepository userRepo)
    {
        _userRepo = userRepo;
    }

    public async Task<User> FindByIdAsync(string userId, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var user = await _userRepo.GetOneAsync(userId);
            if (user != null)
            {
                return user;
            }

            return null;
        }
}

Then you need to write UT for this class然后你需要为这个 class 写 UT

public class Test
{
    [Fact]
    public async Task Find_By_Id_Test()
    {
        // Arrange
        // Your mock here
        var mockMongoUserStore = Mock<IUserStore<ApplicationUser>>();
        // Act
        var user = await mockMongoUserStore.FindByIdAsync("abc", null);
        // Assert
        Assert.True(user != null);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM