我对我的api进行了所有更改,以使用具有this和this链接功能的Azure广告,但是在部署api后,我需要使用户获得网址{{3} }(例如)将其重定向到azure广告登录,然后知道客户端是否有权使用此api,然后再次将其重定向到我的api并显示他有权访问的点。
我在startup.cs上进行了一些更改以使用OpenIdConnect
//Add AddAzureAdBearer Auth options
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = OpenIdConnectDefaults.AuthenticationScheme;
//options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddOpenIdConnect(option =>
{
option.ClientId = Client;
option.Authority = $"{Instance}/{Tenant}";
option.SignedOutRedirectUri = "https://localhost:44308";
option.AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet;
option.SaveTokens = true;
option.Events = new OpenIdConnectEvents
{
OnRemoteFailure = context =>
{
context.HandleResponse();
return Task.CompletedTask;
}
};
})
.AddCookie()
.AddAzureAdBearer(options => _configuration.Bind("Ad", options));
然后我添加了一个HomeController来重定向到swagger UI:
[Authorize]
public class HomeController : Controller
{
[HttpGet("")]
public ActionResult Index()
{
return Redirect("~/swagger");
}
}
当我启动api时,它可以正常运行,但是当您写https://{host:port}/swagger
时它不起作用,请不要执行身份验证过程并自动进入https://{host:port}/swagger/index.html
。
我该如何解决?
我正在使用Net Core 2.0和Swashbuckle进行扩展。
答案 0 :(得分:0)
您需要在应用程序的Startup.cs文件的ConfigureServices(IServiceCollection services)
和Configure(IApplicationBuilder app, IHostingEnvironment env)
中添加Swagger支持。为此,您需要创建一个SwaggerServiceExtensions类,并添加必要的代码以在您的应用程序中支持Swagger。
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger;
namespace JwtSwaggerDemo.Infrastructure
{
public static class SwaggerServiceExtensions
{
public static IServiceCollection AddSwaggerDocumentation(this IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1.0", new Info { Title = "Main API v1.0", Version = "v1.0" });
c.AddSecurityDefinition("Bearer", new ApiKeyScheme
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = "header",
Type = "apiKey"
});
});
return services;
}
public static IApplicationBuilder UseSwaggerDocumentation(this IApplicationBuilder app)
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1.0/swagger.json", "Versioned API v1.0");
c.DocExpansion("none");
});
return app;
}
}
}
Startup.cs文件中的更改
使用上述类,您在Startup.cs文件中只需要做的是以下操作:
namespace JwtSwaggerDemo
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//... rest of services configuration
services.AddSwaggerDocumentation();
//...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
//.... rest of app configuration
app.UseSwaggerDocumentation();
}
//.... rest of app configuration
}
}
}
在Swagger UI中授权请求 现在,当您加载Swagger的用户界面地址(例如:https://localhost:44321/swagger/#/)时,您会在顶部看到一个“授权”按钮。单击它会打开一个模式窗口,通过在值输入字段中添加Bearer,您可以使用JWT令牌授权您的应用。