Startup.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Authentication.JwtBearer;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.AspNetCore.HttpsPolicy;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Microsoft.Extensions.Configuration;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.Hosting;
  13. using Microsoft.Extensions.Logging;
  14. using Microsoft.IdentityModel.Tokens;
  15. namespace AzureAuth
  16. {
  17. public class Startup
  18. {
  19. public Startup(IConfiguration configuration)
  20. {
  21. Configuration = configuration;
  22. }
  23. public IConfiguration Configuration { get; }
  24. // This method gets called by the runtime. Use this method to add services to the container.
  25. public void ConfigureServices(IServiceCollection services)
  26. {
  27. services.AddControllers();
  28. services.AddAuthentication()
  29. .AddJwtBearer(options => //AzureADJwtBearer CN
  30. {
  31. //options.SaveToken = true; //驗證令牌由服務器生成才有效,不適用於服務重啟或分布式架構
  32. options.Authority = "https://login.chinacloudapi.cn/4807e9cf-87b8-4174-aa5b-e76497d7392b/v2.0";
  33. options.Audience = "72643704-b2e7-4b26-b881-bd5865e7a7a5";
  34. options.RequireHttpsMetadata = true;
  35. options.TokenValidationParameters = new TokenValidationParameters
  36. {
  37. RoleClaimType = "roles",
  38. ValidAudiences = new string[] { "72643704-b2e7-4b26-b881-bd5865e7a7a5", "api://72643704-b2e7-4b26-b881-bd5865e7a7a5" },
  39. };
  40. options.Events = new JwtBearerEvents();
  41. //下列事件有需要紀錄則打開
  42. //options.Events.OnMessageReceived = async context => { await Task.FromResult(0); };
  43. //options.Events.OnForbidden = async context => { await Task.FromResult(0); };
  44. //options.Events.OnChallenge = async context => { await Task.FromResult(0); };
  45. options.Events.OnAuthenticationFailed = async context =>
  46. {
  47. await Task.FromResult(0);
  48. };
  49. options.Events.OnTokenValidated = async context =>
  50. {
  51. if (!context.Principal.Claims.Any(x => x.Type == "http://schemas.microsoft.com/identity/claims/scope") //ClaimConstants.Scope
  52. && !context.Principal.Claims.Any(y => y.Type == "roles")) //ClaimConstants.Roles //http://schemas.microsoft.com/ws/2008/06/identity/claims/role
  53. {
  54. //TODO 需處理額外授權非角色及範圍的訪問異常紀錄
  55. throw new UnauthorizedAccessException("Neither scope or roles claim was found in the bearer token.");
  56. }
  57. await Task.FromResult(0);
  58. };
  59. });
  60. }
  61. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  62. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  63. {
  64. if (env.IsDevelopment())
  65. {
  66. app.UseDeveloperExceptionPage();
  67. }
  68. app.UseHttpsRedirection();
  69. app.UseRouting();
  70. app.UseAuthorization();
  71. app.UseEndpoints(endpoints =>
  72. {
  73. endpoints.MapControllers();
  74. });
  75. }
  76. }
  77. }