• [转载]【Abp VNext】实战入门(七):【1】xxx.Web网站层 —— API接口服务添加跨域访问支持


    添加指定跨域支持

    private const string DefaultCorsPolicyName = "Default";
    public override void ConfigureServices(ServiceConfigurationContext context)
            {
                var hostingEnvironment = context.Services.GetHostingEnvironment();
                var configuration = context.Services.GetConfiguration();
                //............................
                //添加跨域支持:原有代码不用动
                context.Services.AddCors(options =>
                {
                    options.AddPolicy(DefaultCorsPolicyName , builder =>
                    {
                        builder.WithOrigins(
                                configuration["App:CorsOrigins"]
                                    .Split(",", StringSplitOptions.RemoveEmptyEntries)
                                    .Select(o => o.RemovePostFix("/"))
                                    .ToArray()
                            )
                            .WithAbpExposedHeaders()
                            .SetIsOriginAllowedToAllowWildcardSubdomains()
                            .AllowAnyHeader()
                            .AllowAnyMethod()
                            .AllowCredentials();
                    });
                });
            }
    
            public override void OnApplicationInitialization(ApplicationInitializationContext context)
            {
                var app = context.GetApplicationBuilder();
                var env = context.GetEnvironment();             
     
                //必须放在下面2个的前面才有用 不然还是报跨域错误:
                app.UseCors(DefaultCorsPolicyName);
                app.UseIdentityServer();            
                app.UseAuthorization();           
            }

    添加所有跨域支持:简单粗暴疗效好

    private const string DefaultCorsPolicyName = "Default";
    public override void ConfigureServices(ServiceConfigurationContext context)
            {
                var hostingEnvironment = context.Services.GetHostingEnvironment();
                var configuration = context.Services.GetConfiguration();
                //............................
                //添加跨域支持:原有代码不用动
                context.Services.AddCors(options =>
                {
                    options.AddPolicy(DefaultCorsPolicyName , builder =>
                    {
                        builder
                            .AllowAnyOrigin()//添加这个
                            .WithAbpExposedHeaders()
                            .SetIsOriginAllowedToAllowWildcardSubdomains()
                            .AllowAnyHeader()
                            .AllowAnyMethod();
                            //.AllowCredentials(); 去掉这个
                    });
                });
            }
    
            public override void OnApplicationInitialization(ApplicationInitializationContext context)
            {
                var app = context.GetApplicationBuilder();
                var env = context.GetEnvironment();             
     
                //必须放在下面2个的前面才有用 不然还是报跨域错误:
                app.UseCors(DefaultCorsPolicyName);
                app.UseIdentityServer();            
                app.UseAuthorization();           
            }
  • 相关阅读:
    23种设计模式
    doraemon的python Flask框架 websocket和redis
    doraemon的python Flask框架 路由和配置
    doraemon的python Flask框架 安装以及基础应用
    doraemon的python centos的入门(五)用户和用户组权限
    doraemon的python centos的入门(四)查询和压缩文件、文件夹
    doraemon的python centos的入门(三)vim
    doraemon的python centos的入门(二)文件目录操作
    doraemon的python centos的入门(一)增删改查命令
    doraemon的python CRM项目中公户与私户转换、搜索条件的应用
  • 原文地址:https://www.cnblogs.com/sui84/p/15616876.html
Copyright © 2020-2023  润新知