|
@@ -1,15 +1,26 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.IdentityModel.Tokens.Jwt;
|
|
|
using System.Linq;
|
|
|
+using System.Security.Claims;
|
|
|
+using System.Text.Json;
|
|
|
using System.Threading.Tasks;
|
|
|
using Grpc.Extension.AspNetCore;
|
|
|
+using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
+using Microsoft.Extensions.Primitives;
|
|
|
+using Microsoft.IdentityModel.Tokens;
|
|
|
using TEAMModelGrpc.Services;
|
|
|
+using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
|
|
|
+using TEAMModelOS.SDK.Module.AzureCosmosDB.Configuration;
|
|
|
+using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
|
|
|
+using TEAMModelOS.SDK.Module.AzureTable.Implements;
|
|
|
+using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
|
|
|
|
|
|
namespace TEAMModelGrpc
|
|
|
{
|
|
@@ -27,36 +38,93 @@ namespace TEAMModelGrpc
|
|
|
services.AddGrpc();
|
|
|
//添加Grpc扩展
|
|
|
services.AddGrpcExtensions(_conf);
|
|
|
+
|
|
|
+ services.AddAuthorization(options =>
|
|
|
+ {
|
|
|
+ options.AddPolicy(JwtBearerDefaults.AuthenticationScheme, policy =>
|
|
|
+ {
|
|
|
+ policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme);
|
|
|
+ policy.RequireClaim(ClaimTypes.Name);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
+ .AddJwtBearer(options =>
|
|
|
+ {
|
|
|
+ options.TokenValidationParameters =
|
|
|
+ new TokenValidationParameters
|
|
|
+ {
|
|
|
+ ValidateAudience = false,
|
|
|
+ ValidateIssuer = false,
|
|
|
+ ValidateActor = false,
|
|
|
+ ValidateLifetime = true,
|
|
|
+ IssuerSigningKey = SecurityKey
|
|
|
+ };
|
|
|
+ });
|
|
|
+
|
|
|
+ // Table配置
|
|
|
+ services.AddScoped<IAzureTableDBRepository, AzureTableDBRepository>();
|
|
|
+ //使用Blob配置
|
|
|
+ services.AddAzureBlobStorage().AddConnection(_conf.GetSection("Azure:Blob").Get<AzureBlobOptions>());
|
|
|
+ //使用CosmosDB
|
|
|
+ services.AddAzureCosmosDBV3().AddCosmosDBV3Connection(_conf.GetSection("Azure:CosmosDB").Get<AzureCosmosDBOptions>())
|
|
|
+ .AddCosmosSerializer(new SystemTextJsonCosmosSerializer(new JsonSerializerOptions() { IgnoreNullValues = true }));
|
|
|
+
|
|
|
}
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
+ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IAzureCosmosDBV3Repository cosmosDBV3Repository)
|
|
|
{
|
|
|
if (env.IsDevelopment())
|
|
|
{
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
}
|
|
|
-
|
|
|
+ cosmosDBV3Repository.InitializeDatabase();
|
|
|
app.UseRouting();
|
|
|
|
|
|
+ //注册 ASP.NET Core 身份验证中间件的顺序很重要。
|
|
|
+ //始终在 UseRouting 之后和 UseEndpoints 之前调用 UseAuthentication 和 UseAuthorization。
|
|
|
+ app.UseAuthentication();
|
|
|
+ app.UseAuthorization();
|
|
|
+
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
{
|
|
|
endpoints.MapGrpcService<GreeterService>();
|
|
|
|
|
|
+ endpoints.MapGet("/generateJwtToken", context =>
|
|
|
+ {
|
|
|
+ return context.Response.WriteAsync(GenerateJwtToken(context.Request.Query["name"]));
|
|
|
+ });
|
|
|
+
|
|
|
endpoints.MapGet("/", async context =>
|
|
|
{
|
|
|
await context.Response.WriteAsync("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");
|
|
|
});
|
|
|
});
|
|
|
//CodeFirst的Grpc(会自动扫描TStartup所在程序集下的IGrpcSerivce)
|
|
|
- app.UseGrpcExtensions<KnowledgeService>(options =>
|
|
|
+ app.UseGrpcExtensions<TEAMModelGrpc.Services.KnowledgeService>(options =>
|
|
|
{
|
|
|
//CodeFirst配制
|
|
|
options.GlobalPackage = "math";
|
|
|
- options.ProtoNameSpace = "TEAMModelGrpc";
|
|
|
+ options.ProtoNameSpace = "TMDGrpc";
|
|
|
})
|
|
|
//CodeFirst生成proto
|
|
|
.UseProtoGenerate("protos", false);
|
|
|
}
|
|
|
+
|
|
|
+ private string GenerateJwtToken(string name)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrEmpty(name))
|
|
|
+ {
|
|
|
+ throw new InvalidOperationException("Name is not specified.");
|
|
|
+ }
|
|
|
+
|
|
|
+ var claims = new[] { new Claim(ClaimTypes.Name, name) };
|
|
|
+ var credentials = new SigningCredentials(SecurityKey, SecurityAlgorithms.HmacSha256);
|
|
|
+ var token = new JwtSecurityToken("ExampleServer", "ExampleClients", claims, expires: DateTime.Now.AddSeconds(60), signingCredentials: credentials);
|
|
|
+ return JwtTokenHandler.WriteToken(token);
|
|
|
+ }
|
|
|
+
|
|
|
+ private readonly JwtSecurityTokenHandler JwtTokenHandler = new JwtSecurityTokenHandler();
|
|
|
+ private readonly SymmetricSecurityKey SecurityKey = new SymmetricSecurityKey(Guid.NewGuid().ToByteArray());
|
|
|
}
|
|
|
}
|