基于Docker for windows部署C#.net Core后端服务如何调用其它认证服务

public class Startup

{

public Startup(IConfiguration configuration)

{

Configuration = configuration;

}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.

public void ConfigureServices(IServiceCollection services)

{

services.Configure<Configure>(Configuration.GetSection("Token"));

services.AddControllers();

var serverVersion = new MySqlServerVersion(new Version(8, 0, 25));

services.AddDbContext<DBServiceContext>(options =>

{

options.UseMySql(Configuration.GetConnectionString("DbConnection"), serverVersion);

});

services.AddSwaggerGen(c =>

{

c.SwaggerDoc("v1", new OpenApiInfo { Title = "一板一码模拟后台接口", Version = "v1" });

//c.IncludeXmlComments(string.Format("{0}/RunnerService.xml", AppDomain.CurrentDomain.BaseDirectory));

});

services.AddAuthentication(options =>

{

options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;

options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

}).AddJwtBearer(x =>

{

x.RequireHttpsMetadata = false;

x.SaveToken = true;

x.TokenValidationParameters = new TokenValidationParameters

{

ValidateIssuerSigningKey = true,//是否调用对签名securityToken的SecurityKey进行验证

IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration.GetSection("Token").GetSection("Secret").Value.ToString())),//签名秘钥

ValidateIssuer = true,//是否验证颁发者

ValidIssuer = Configuration.GetSection("Token").GetSection("Issuer").Value.ToString(),

ValidateAudience = true, //是否验证接收者

ValidAudience = Configuration.GetSection("Token").GetSection("Audience").Value.ToString(),//接收者

ValidateLifetime = true,//是否验证失效时间

};

});

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{

if (env.IsDevelopment())

{

app.UseDeveloperExceptionPage();

//app.UseSwagger();

//app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "RunnerService v1"));

//app.UseSwaggerUI(c =>

//{

// c.SwaggerEndpoint("/swagger/v1/swagger.json", "RunnerService v1");

// c.RoutePrefix = string.Empty;//设置根节点访问

//});

}

app.UseSwagger();

app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Service v1"));

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthentication();

app.UseAuthorization();

app.UseEndpoints(endpoints =>

{

endpoints.MapControllers();

});

}

}

配置数据

{

"Logging": {

"LogLevel": {

"Default": "Information",

"Microsoft": "Warning",

"Microsoft.Hosting.Lifetime": "Information"

}

},

"AllowedHosts": "*",

"ConnectionStrings": {

"DbConnection": "server=ip地址;port=3306;database=数据库名称;user=root;password=123456;"

},

"Token": {

"Issuer": "test",

"Audience": "test",

"Secret": "123456",

"AccessExpiration": "30"

}

}

相关推荐
Hello.Reader3 分钟前
从 0 到 1 理解硬盘数据恢复工具原理与工程实现
linux·运维·服务器·网络·数据库
小坏坏的大世界1 小时前
VMware 虚拟机无法上网问题排查
服务器·网络
切糕师学AI3 小时前
NAT (Network Address Translation,网络地址转换)
运维·服务器·网络
github.com/starRTC3 小时前
Claude Code中英文系列教程34:再谈Skills
开发语言·c#·ai编程
Channing Lewis4 小时前
为什么部署的项目会耗尽系统句柄
服务器
bugcome_com5 小时前
C# 判断语句详解(if、switch、三目运算符、Null 条件运算符)
c#
山峰哥5 小时前
SQL调优实战:从索引失效到性能飙升的破局之道
服务器·数据库·sql·性能优化·编辑器·深度优先
玩具猴_wjh5 小时前
JWT优化方案
java·服务器·数据库
盟接之桥6 小时前
制造业EDI数字化:连接全球供应链的桥梁
linux·运维·服务器·网络·人工智能·制造
2501_926978336 小时前
分形时空理论框架:从破缺悖论到意识宇宙的物理学新范式引言(理论概念版)--AGI理论系统基础1.1
java·服务器·前端·人工智能·经验分享·agi