Asp.Netcoreドメイン間設定

24150 ワード

環境の検証:
dotnet core 2.1/Asp.net core2.1
 
一、作用域がミドルウェア層にある
配置の仕方はstartup.csファイルConfigure(IApplicationBuilder app,IHostingEnvironment env)メソッドでドメイン間構成を追加します.公式の例:
 1    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
 2         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 3         {
 4             if (env.IsDevelopment())
 5             {
 6                 app.UseDeveloperExceptionPage();
 7             }
 8 
 9          
10             app.UseCors(builder => builder.WithOrigins("http://example.com"));
11             
12             app.UseMvc();
13         }

appを使用します.UseCors(builder =>builder.WithOrigins("http://example.com"));"http://example.com「ドメイン間のアドレスを許可するために、WithOriginsは複数のアドレスをサポートできます.
公式説明UseCorsメソッドの設定はapp.UserMvcまたはapp.Run前.
 
二、ドメイン間ポリシー定義
startup.csファイルConfigureServices(IServiceCollection services)メソッドでポリシーを定義し、複数のポリシーの定義をサポートします.公式の例:
 
  1 using System;
  2 using Microsoft.AspNetCore.Builder;
  3 using Microsoft.AspNetCore.Hosting;
  4 using Microsoft.AspNetCore.Http;
  5 using Microsoft.Extensions.DependencyInjection;
  6 using Microsoft.Extensions.Logging;
  7 
  8 namespace CorsExample4
  9 {
 10     public class Startup
 11     {
 12         // This method gets called by the runtime. Use this method to add services to the container.
 13         // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
 14         public void ConfigureServices(IServiceCollection services)
 15         {
 16             services.AddCors(options =>
 17             {
 18                 // BEGIN01
 19                 options.AddPolicy("AllowSpecificOrigins",
 20                 builder =>
 21                 {
 22                     builder.WithOrigins("http://example.com", "http://www.contoso.com");
 23                 });
 24                 // END01
 25 
 26                 // BEGIN02
 27                 options.AddPolicy("AllowAllOrigins",
 28                     builder =>
 29                     {
 30                         builder.AllowAnyOrigin();
 31                     });
 32                 // END02
 33 
 34                 // BEGIN03
 35                 options.AddPolicy("AllowSpecificMethods",
 36                     builder =>
 37                     {
 38                         builder.WithOrigins("http://example.com")
 39                                .WithMethods("GET", "POST", "HEAD");
 40                     });
 41                 // END03
 42 
 43                 // BEGIN04
 44                 options.AddPolicy("AllowAllMethods",
 45                     builder =>
 46                     {
 47                         builder.WithOrigins("http://example.com")
 48                                .AllowAnyMethod();
 49                     });
 50                 // END04
 51 
 52                 // BEGIN05
 53                 options.AddPolicy("AllowHeaders",
 54                     builder =>
 55                     {
 56                         builder.WithOrigins("http://example.com")
 57                                .WithHeaders("accept", "content-type", "origin", "x-custom-header");
 58                     });
 59                 // END05
 60 
 61                 // BEGIN06
 62                 options.AddPolicy("AllowAllHeaders",
 63                     builder =>
 64                     {
 65                         builder.WithOrigins("http://example.com")
 66                                .AllowAnyHeader();
 67                     });
 68                 // END06
 69 
 70                 // BEGIN07
 71                 options.AddPolicy("ExposeResponseHeaders",
 72                     builder =>
 73                     {
 74                         builder.WithOrigins("http://example.com")
 75                                .WithExposedHeaders("x-custom-header");
 76                     });
 77                 // END07
 78 
 79                 // BEGIN08
 80                 options.AddPolicy("AllowCredentials",
 81                     builder =>
 82                     {
 83                         builder.WithOrigins("http://example.com")
 84                                .AllowCredentials();
 85                     });
 86                 // END08
 87 
 88                 // BEGIN09
 89                 options.AddPolicy("SetPreflightExpiration",
 90                     builder =>
 91                     {
 92                         builder.WithOrigins("http://example.com")
 93                                .SetPreflightMaxAge(TimeSpan.FromSeconds(2520));
 94                     });
 95                 // END09
 96             });
 97         }
 98 
 99         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
100         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
101         {
102             loggerFactory.AddConsole();
103 
104             if (env.IsDevelopment())
105             {
106                 app.UseDeveloperExceptionPage();
107             }
108 
109             app.UseCors("AllowSpecificOrigins");
110             app.Run(async (context) =>
111             {
112                 await context.Response.WriteAsync("Hello World!");
113             });
114         }
115     }
116 }

appを使用します.UseCors("AllowSpecificOrigins");特定のドメイン間ポリシーを呼び出します.「AllowSpecificOrigins」はポリシー名で、ドメイン間役割ドメインは中間層にあります.ポリシーの定義と使用方法の詳細は、公式の参照文書(本明細書では最後にアドレスを示す)を参照してください.
三、作用域がMVC層にある
MVCを使用する場合、公式に与えられた3つの設定方式は、Action前設定、Controller前設定、グローバル設定である.
  • Action

  • Actionメソッドの前にEnableCors(ポリシー名)のタグを追加する.公式の例
    1 [HttpGet]
    2 [EnableCors("AllowHeaders")]
    3 public IEnumerable<string> Get()
    4 {
    5     return new string[] { "value1", "value2" };
    6 }

    EnableCorsはMicrosoftにあります.AspNetCore.Corsネーミングスペースの下.「AllowHeaders」はポリシー名です.
     
  • Controller

  •  
    Controllerの前にEnableCors(ポリシー名)のタグを追加します.公式の例
     
    [EnableCors("AllowSpecificOrigin")]
    public class ValuesController : Controller

     
  • MVCグローバル(Globally)
  • 公式の説明では、すべてのControllerにドメイン間設定を追加するには、「C o r s AuthorizationFilterFactory」フィルタを使用します.公式の例:
     1 using Microsoft.AspNetCore.Mvc.Cors.Internal;
     2 
     3 ...
     4 
     5 public void ConfigureServices(IServiceCollection services)
     6 {
     7     services.AddCors(options =>
     8     {
     9      //...    ...
    10      });
    11 
    12     services.AddMvc();
    13     services.Configure(options =>
    14     {
    15         options.Filters.Add(new CorsAuthorizationFilterFactory("AllowAllMethods"));
    16     });
    17 }

    CorsAuthorizationFilterFactoryは名前空間でMicrosoft.AspNetCore.Mvc.Cors.Internal下.「AllowAllMethods」はポリシー名です.
     
  • ドメイン間
  • の無効化
    公式の説明では、「DisableCors」というタグを使用してアクションまたはControllerのドメイン間設定を設定することはできません.公式の例:
     
    1 [HttpGet("{id}")]
    2 [DisableCors]
    3 public string Get(int id)
    4 {
    5     return "value";
    6 }

    DisableCorsはネーミングスペースでMicrosoft.AspNetCore.Cors下.
     
    四、全体作用範囲
    役割範囲、Middleware>Globally>Controller>Action.
    有効優先順位はAction,Controller,Globally,Middlewareである.すなわち、Actionは、ドメイン間優先コントローラの有効化、コントローラ優先Globally、Globally優先Middlewareを定義する.
    ドメイン間定義が有効でない場合は、ActionとControllerおよびControllerベースクラスが他のドメイン間設定を定義しているかどうかを確認します.
     
     
     
     
    公式参考記事:https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-2.1
    転載先:https://www.cnblogs.com/hobinly/p/9437143.html