|
| 1 | +using Microsoft.AspNetCore.Builder; |
| 2 | +using Microsoft.AspNetCore.Hosting; |
| 3 | +using Microsoft.AspNetCore.Http; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Hosting; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using AuthLibrary.Services; |
| 12 | +using Microsoft.IdentityModel.Tokens; |
| 13 | + |
| 14 | +namespace AuthGrpcService |
| 15 | +{ |
| 16 | + public class Startup |
| 17 | + { |
| 18 | + const string BizBook365Com = "bizbook365.com"; |
| 19 | + private const string SecretKey = "a55ae165-912d-4052-b61e-aeeb6d6d2c07"; |
| 20 | + private readonly SymmetricSecurityKey SigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SecretKey)); |
| 21 | + |
| 22 | + // This method gets called by the runtime. Use this method to add services to the container. |
| 23 | + // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 |
| 24 | + public void ConfigureServices(IServiceCollection services) |
| 25 | + { |
| 26 | + services.AddSingleton<RedisService>(); |
| 27 | + services.AddGrpc(); |
| 28 | + services.AddAuthorization(); |
| 29 | + services.AddTokenValidation(BizBook365Com, BizBook365Com, SigningKey); |
| 30 | + } |
| 31 | + |
| 32 | + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
| 33 | + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RedisService redisService) |
| 34 | + { |
| 35 | + if (env.IsDevelopment()) |
| 36 | + { |
| 37 | + app.UseDeveloperExceptionPage(); |
| 38 | + } |
| 39 | + |
| 40 | + app.UseRouting(); |
| 41 | + |
| 42 | + app.UseAuthentication(); |
| 43 | + app.UseAuthorization(); |
| 44 | + |
| 45 | + redisService.Connect(); |
| 46 | + |
| 47 | + app.UseEndpoints(endpoints => |
| 48 | + { |
| 49 | + endpoints.MapGrpcService<GreeterService>(); |
| 50 | + |
| 51 | + endpoints.MapGet("/", async context => |
| 52 | + { |
| 53 | + 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"); |
| 54 | + }); |
| 55 | + }); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments