【发布时间】: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