【问题标题】:Bot Framework SDK v4 OAuth with GithubBot Framework SDK v4 OAuth 与 Github
【发布时间】:2018-10-22 19:51:56
【问题描述】:

我想创建一个可以通过 GitHub 进行身份验证的机器人,并可以访问用户存储库并通过任何 PR 通知用户等。我已经尝试了 Microsoft 列出的here 提供的身份验证示例。 但我不明白如何更改它以满足我的需要。

我在我的应用程序中实现了如下 OAuth,因为它是一个 dotnet 核心应用程序。

public void ConfigureServices(IServiceCollection services)
    {

        // Set up the service configuration
        var builder = new ConfigurationBuilder()
            .SetBasePath(ContentRootPath)
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables();


        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = "GitHub";
        })
        .AddCookie()
        .AddOAuth("GitHub", options =>
        {
            options.ClientId = Configuration["GitHub:ClientId"];
            options.ClientSecret = Configuration["GitHub:ClientSecret"];
            options.CallbackPath = new PathString("/signin_github");

            options.AuthorizationEndpoint = "https://github.com/login/oauth/authorize";
            options.TokenEndpoint = "https://github.com/login/oauth/access_token";
            options.UserInformationEndpoint = "https://api.github.com/user";

            options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
            options.ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
            options.ClaimActions.MapJsonKey("urn:github:login", "login");
            options.ClaimActions.MapJsonKey("urn:github:url", "html_url");
            options.ClaimActions.MapJsonKey("urn:github:avatar", "avatar_url");

            options.Events = new OAuthEvents
            {
                OnCreatingTicket = async context =>
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);

                    var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
                    response.EnsureSuccessStatusCode();

                    var user = JObject.Parse(await response.Content.ReadAsStringAsync());

                    context.RunClaimActions(user);
                }
            };
        });

        var configuration = builder.Build();
        services.AddSingleton(configuration);
        // Add your SimpleBot to your application
        services.AddBot<RichCardsBot>(options =>
        {
            options.CredentialProvider = new ConfigurationCredentialProvider(configuration);
        });


    }

我不明白如何从我的机器人 UI 中调用它。任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: c# oauth-2.0 .net-core botframework github-api


    【解决方案1】:

    看起来您在 bot 框架中的工作方式错误。你应该关注this guide。该指南介绍了如何设置 AAD,但仍涵盖了如何在 Azure 门户中设置 OAuth 连接的基础知识。

    要设置 github,您将单击机器人频道注册设置选项卡中的添加连接按钮并填写所需信息,如下所示:

    在您的机器人中,您必须向用户发送 OAuth 卡,如示例中所示,假设您的连接已在 Azure 门户中正确设置,则 OAuth 流程的其余部分将由框架处理。

    【讨论】:

    • 非常感谢。这就是我一直在寻找的。​​span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2019-07-27
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多