代码拉取完成,页面将自动刷新
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using GrpcServer_Demo;
using GrpcServer_Demo.Services;
using inter;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.IdentityModel.Tokens;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(x => { x.ListenAnyIP(5093, listenOptions => { listenOptions.Protocols = HttpProtocols.Http2; }); });
builder.Services.AddHttpContextAccessor();
builder.Services.AddGrpc(o => { o.Interceptors.Add<ParamInterceptor>(); });
builder.Services.AddAuthentication()
.AddJwtBearer(options =>
{
options.TokenValidationParameters =
new TokenValidationParameters
{
ValidateAudience = false,
ValidateIssuer = false,
ValidateActor = false,
ValidateLifetime = true,
IssuerSigningKey = GlobalConst.SecurityKey
};
});
builder.Services.AddGrpcClient<InternalService.InternalServiceClient>(o => { o.Address = new Uri("http://localhost:5093"); });
builder.Services.AddAuthorization(options =>
{
options.AddPolicy(JwtBearerDefaults.AuthenticationScheme, policy =>
{
policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme);
policy.RequireClaim(ClaimTypes.Name);
});
//添加一个策略
// options.AddPolicy(GlobalConst.AdminPolicy,
// p => { p.RequireClaim(GlobalConst.RoleClaim, "admin"); });
});
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapGrpcService<UserService>();
app.MapGrpcService<InterServices>();
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
app.Run();
public static class GlobalConst
{
public const string AdminPolicy = "admin";
public const string RoleClaim = "role";
public static readonly SymmetricSecurityKey SecurityKey = new SymmetricSecurityKey("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a clientCommunication with gRPC endpoints must be made through a gRPC client. To learn how to create a clientCommunication with gRPC endpoints must be made through a gRPC client. To learn how to create a client"u8.ToArray());
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。