点击时激活Azure Ad身份验证https://host:port/swagger 在NetCore2API上?
我对我的api进行了所有更改以使用Azure Ad和链接功能,但在部署api时,我需要让获取Url的用户(例如)将其重定向到Azure Ad登录,然后知道客户端是否有权使用此api,并再次将其重定向到我的api,并显示他有权访问的enpoints 我在startup.cs上做了一些更改以使用OpenIdConnect点击时激活Azure Ad身份验证https://host:port/swagger 在NetCore2API上?,azure,azure-active-directory,asp.net-core-2.0,asp.net-core-webapi,Azure,Azure Active Directory,Asp.net Core 2.0,Asp.net Core Webapi,我对我的api进行了所有更改以使用Azure Ad和链接功能,但在部署api时,我需要让获取Url的用户(例如)将其重定向到Azure Ad登录,然后知道客户端是否有权使用此api,并再次将其重定向到我的api,并显示他有权访问的enpoints 我在startup.cs上做了一些更改以使用OpenIdConnect //Add AddAzureAdBearer Auth options services.AddAuthentication(options =>
//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时,它可以像spected一样工作,但是当我写https://{host:port}/swagger
时,它不工作,不要点击身份验证过程,并自动转到https://{host:port}/swagger/index.html
我怎样才能解决这个问题
我正在使用net core 2.0和Swashback进行招摇过市。您需要将招摇过市支持添加到应用程序的Startup.cs文件中的
ConfigureServices(IServiceCollection services)
和配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
。为此,您需要创建一个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的UI地址(例如:)时,您将在顶部看到一个授权按钮。点击它会出现一个模式窗口,通过在值输入字段中添加载体,您可以使用JWT令牌对应用程序进行授权。这不是我的意思。这个答案是我使用的第一种方法,但它不是我想要的。当我打开地址栏并按enter键时,我需要重定向到microsoft登录页面。现在,我的Api默认url()将我重定向到登录,但当我添加/swagger时,它不会请求登录并显示Api页面。这就是我的问题,我描述了我使用的代码。非常感谢。