大海中一粒沙子 2020-08-02 09:58 采纳率: 0%
浏览 8

.net core如何做跨域配置

1、网上找了好多方法都有问题,也不生效
2、哪位大神知道的,给个回复,重金酬谢

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-25 11:07
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在.NET Core中,为了实现跨域访问,你需要使用Microsoft.AspNetCore.Authentication.JwtBearerMicrosoft.AspNetCore.Identity.EntityFrameworkCore。这两个框架提供了支持JWT身份验证的功能。

    以下是一个简单的示例:

    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.HttpsPolicy;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    using Microsoft.IdentityModel.Tokens;
    using System.Text;
    
    namespace MyProject
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers();
                services.AddDbContext<YourDbContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
                // 添加JwtBearer认证
                services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                    .AddJwtBearer(options => 
                        options.TokenValidationParameters = new TokenValidationParameters()
                        {
                            ValidateIssuerSigningKey = true,
                            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Secret"])),
                            ValidateIssuer = false,
                            ValidateAudience = false
                        });
    
                // 添加Identity核心
                services.AddIdentityCore<ApplicationUser>()
                    .AddEntityFrameworkStores<YourDbContext>();
    
                // 添加JWT令牌生成器
                services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                    .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
                    {
                        options.RequireHttpsMetadata = false;
                        options.SaveToken = true;
                        options.TokenValidationParameters = new TokenValidationParameters
                        {
                            ValidateIssuer = false,
                            ValidIssuer = "https://localhost:7059",
                            ValidateAudience = false,
                            ValidAudience = "https://localhost:7059"
                        };
                    });
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
    
                app.UseHttpsRedirection();
                app.UseStaticFiles();
    
                app.UseRouting();
    
                app.UseAuthentication();
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
        }
    }
    

    在这个例子中,我们首先添加了JWT身份验证和JWT令牌生成器。然后,我们为应用程序添加了一个JWT令牌验证器,它告诉JWT令牌生成器应该信任哪个发件人(这里是我们自己的应用)。

    最后,我们在Configure方法中设置了开发环境下的异常处理,并在生产环境中设置了错误处理和HTTPS保护。

    注意:这个例子只是一个基本的示例,你可能需要根据你的具体需求进行调整。例如,你可能需要添加更多的安全措施,或者在开发环境中启用HTTPS等。

    评论

报告相同问题?

悬赏问题

  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥35 跳过我的世界插件ip验证
  • ¥15 抖音看过的视频,缓存在哪个文件
  • ¥15 自定义损失函数报输入参数的数目不足
  • ¥15 如果我想学习C大家有是的的资料吗
  • ¥15 根据文件名称对文件进行排序
  • ¥15 deploylinux的ubuntu系统无法成功安装使用MySQL❓
  • ¥15 有人会用py或者r画这种图吗
  • ¥15 MOD04_3K图像预处理