简体   繁体   English

自定义商店中的Asp.Net Core身份异步开销

[英]Asp.Net Core Identity async Overhead in Custom Stores

While developing a Asp.Net Core Web Application I needed to add Asp.Net Core Identity to my Project. 在开发Asp.Net Core Web应用程序时,我需要将Asp.Net Core Identity添加到我的项目中。 However I needed only a small part of Identity because eg I know that this application will never allow third party logins. 但是,我只需要Identity的一小部分,因为例如,我知道此应用程序将永远不允许第三方登录。 Therefore I decided to write my custom stores. 因此,我决定写我的海关商店。 While Searching the Internet for good Solutions on how to do that I often saw a Implementation like this: 在Internet上搜索有关如何执行此操作的好的解决方案时,我经常看到这样的实现:

    public Task SetUserNameAsync(ApplicationUser user, string userName, CancellationToken cancellationToken)
    {
        cancellationToken.ThrowIfCancellationRequested();

        if (user == null)
            throw new ArgumentNullException(nameof(user));

        if(string.IsNullOrWhiteSpace(userName))
            throw new ArgumentNullException(nameof(userName));

        user.Username = userName;

        return Task.CompletedTask;
    }

or 要么

    public Task<string> GetUserNameAsync(ApplicationUser user, CancellationToken cancellationToken)
    {
        cancellationToken.ThrowIfCancellationRequested();

        if (user == null)
            throw new ArgumentNullException(nameof(user));

        return Task.FromResult(user.Username);
    }

as far as I understand It that would be called in a fashion like this: 据我了解,这种调用方式如下:

var user = await store.GetUserNameAsync(_user, CancellationToken.None);

but wouldn't that basically create a State Mashine for a getter/setter that should just immediately return without any big overhead or is there any good benefit doing it like this that I am not seeing right now? 但这是否基本上不会为应该立即返回而又没有任何大笔开销的吸气剂/装填剂创建一个州议会,或者这样做有什么好处,而我现在还没有看到?

Identity is a framework that is highly customizable. 身份是一个高度可定制的框架。 Eg you could create your own UserManager while still using the standard SignInManager that handles login, logout, ... 例如,您可以创建自己的UserManager,同时仍使用处理登录,注销等的标准SignInManager。

What you found seems to be implementations of UserManager.GetUserNameAsync()> https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.usermanager-1.getusernameasync?view=aspnetcore-2.1 您发现的似乎是UserManager.GetUserNameAsync()>的实现。https: //docs.microsoft.com/zh-cn/dotnet/api/microsoft.aspnetcore.identity.usermanager-1.getusernameasync?view = aspnetcore-2.1

The reason why this method returns a task is, that the implementation could required db or network access. 该方法返回任务的原因是,该实现可能需要数据库或网络访问。

If you really don't want to use anything of Identity and do everyting yourself, there's no need to return a task. 如果您真的不想使用任何身份标识并自己动手做事,则无需返回任务。 But to be hornest, I'd suggest to think twice if this is a good approach. 但老实说,如果这是一个好方法,我建议三思。

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

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