Bot Framework SDK v4 OAuth Github
Bot Framework SDK v4 OAuth with Github
我想创建一个可以使用 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 调用它。任何帮助将不胜感激。谢谢。
看来您正在以错误的方式在机器人框架中工作。你应该关注 this guide。该指南涵盖了如何设置 AAD,但仍涵盖了如何在 Azure 门户中设置 OAuth 连接的基础知识。
要设置 github,您将在机器人频道注册的设置选项卡中单击添加连接按钮并填写所需信息,它看起来像这样:
在您的机器人中,您将必须向用户发送一张 OAuth 卡,如您在示例中所见,OAuth 流的其余部分将由框架处理,假设您的连接已在 Azure 门户中正确设置。
我想创建一个可以使用 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 调用它。任何帮助将不胜感激。谢谢。
看来您正在以错误的方式在机器人框架中工作。你应该关注 this guide。该指南涵盖了如何设置 AAD,但仍涵盖了如何在 Azure 门户中设置 OAuth 连接的基础知识。
要设置 github,您将在机器人频道注册的设置选项卡中单击添加连接按钮并填写所需信息,它看起来像这样:
在您的机器人中,您将必须向用户发送一张 OAuth 卡,如您在示例中所见,OAuth 流的其余部分将由框架处理,假设您的连接已在 Azure 门户中正确设置。