如何将BotAuthentication注入控制器

时间:2019-01-30 09:10:49

标签: botframework

我正在寻求通过注入使用BotAuthentication的帮助,以便可以使用keyvault的机密来传递MicrosoftAppId和MicrosoftAppPassword。 谢谢。

我正在使用BotBuilder V3.15.3,它允许我使用BotAuthentication并将MicrosoftAppId和MicrosoftAppPassword传递为参数。但是由于我有从keyvault提取的这些值,所以无法使用注释,因为我无法将constv读取keyvault。使用CredentialProviderType的另一项规定为ICredentialProvider类型。 [BotAuthentication(CredentialProviderType = typeof(CustomCredentialProvider))) 但是,此类型不将appId作为参数。如何将appId和appPassword注入其中?

///Controller code

[BotAuthentication(CredentialProviderType =typeof(NewStaticCredentialProvider))]

public class MessagesController : ApiController
    {}

///CredentialProvider code

public interface ICredentialProvider

{

        Task<bool> IsValidAppIdAsync(string appId);

        Task<string> GetAppPasswordAsync(string appId);

        Task<bool> IsAuthenticationDisabledAsync();
    }

    public class SimpleCredentialProvider : ICredentialProvider
    {
        public string AppId { get; set; }

        public string Password { get; set; }

        public Task<bool> IsValidAppIdAsync(string appId)
        {
            return Task.FromResult(appId == AppId);
        }

        public Task<string> GetAppPasswordAsync(string appId)
        {
            return Task.FromResult((appId == this.AppId) ? this.Password : null);
        }

        public Task<bool> IsAuthenticationDisabledAsync()
        {
            return Task.FromResult(string.IsNullOrEmpty(AppId));
        }
    }

    public sealed class NewStaticCredentialProvider : SimpleCredentialProvider
    {
        public NewStaticCredentialProvider(string appId, string password)
        {
            this.AppId = Secrets.MicrosoftAppId;
            this.Password = Secrets.MicrosoftAppPassword;
        }
    }

在注释中,我看不到传递应用程序ID和密码的方法。 我想念什么吗?

1 个答案:

答案 0 :(得分:0)

我已经找到解决方案。问题是我有自己的名称为ICredentialProvider的接口,因此无法解析该实例。我只保留了SimpleCredentialProvider类,并将其更改为其他名称。还有宾果游戏!

相关问题