我一直在使用dotNet core 2.2设置WebApi,我想使用GitHub的OAuth对我的用户进行授权。
我查找并设置了API,以将Swashbuckle.AspNetCore.Swagger
用于其Swagger文档和UI,并使用AspNetCore自己的身份验证来设置OAuth。通过端点auth/signin|out
登录或注销时,一切工作都像灵符一样,但是每当我尝试通过SwaggerUI的“授权”按钮授权客户端时,都会出现错误:
Exception: The oauth state was missing or invalid.
Exception: An error was encountered while handling the remote login.
Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler<TOptions>.HandleRequestAsync()
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
以下是一些我用来设置API内容的代码:
// Setting up Authentication
services
.AddAuthentication(opt =>
{
opt.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = "GitHub";
})
.AddCookie()
.AddGitHub("GitHub", opt =>
{
opt.ClientId = GitHubSettings.ClientId;
opt.ClientSecret = GitHubSettings.ClientSecret;
opt.CallbackPath = new PathString(GitHubSettings.CallbackPath);
opt.SaveTokens = true;
});
// Setting up SwaggerDoc Generation
services.AddSwaggerGen(c =>
{
c.AddSecurityDefinition("GitHub", new OAuth2Scheme
{
Type = "oauth2",
Flow = "accessCode",
AuthorizationUrl = "https://github.com/login/oauth/authorize",
TokenUrl = "https://github.com/login/oauth/access_token"
});
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "NetCore Playground API",
Description = "API para testes com .NET Core 2.2",
Contact = new Contact { Name = "HocusPocus", Url = @"https://google.com", Email = "not.my.email@gmail.com" }
});
});
// Using SwaggerUI
app.UseSwagger();
app.UseSwaggerUI(opt =>
{
opt.DisplayRequestDuration();
opt.RoutePrefix = string.Empty;
opt.SwaggerEndpoint("swagger/v1/swagger.json", "Playground API V1");
opt.OAuth2RedirectUrl("https://localhost:5001/auth/callback");
opt.OAuthClientId(GitHubSettings.ClientId);
opt.OAuthClientSecret(GitHubSettings.ClientSecret);
opt.OAuthAppName("dotNet Core Playground");
});
app.UseAuthentication();
这是针对OpenSource学习项目的,因此该源可在GitHub上找到: https://github.com/rodolphocastro/ARDC.NetCore.Playground/tree/feature/stackoverflow-57086626/src/ARDC.NetCore.Playground
编辑:我已经基于https://www.jerriepelser.com/blog/authenticate-oauth-aspnet-core-2/对代码进行了一些更改,但是使用SwaggerUI的“授权”按钮仍然没有成功。
答案 0 :(得分:0)
更改OAuth2RedirectUrl的端口号-(应用程序端口号)
// Using SwaggerUI
app.UseSwagger();
app.UseSwaggerUI(opt =>
{
opt.DisplayRequestDuration();
opt.RoutePrefix = string.Empty;
opt.SwaggerEndpoint("swagger/v1/swagger.json", "Playground API V1");
opt.OAuth2RedirectUrl("https://localhost:5001/auth/callback");
opt.OAuthClientId(GitHubSettings.ClientId);
opt.OAuthClientSecret(GitHubSettings.ClientSecret);
opt.OAuthAppName("dotNet Core Playground");
});
app.UseAuthentication();