aspdotnetcore 跨域问题
前端是 vue
后端是aspdotnet core 2.2
的 webapi
前端调用后端接口的时候不通:
Access to XMLHttpRequest at 'http://127.0.0.1:13037/api/user/authenticate' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
应该是跨域的问题,不知道的怎样才能让这个'Access-Control-Allow-Origin'
加出来
接口的Startup中应该是已经用了跨域了
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
// configure strongly typed settings objects
var appSettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);
// configure jwt authentication
var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
// https://stackoverflow.com/a/54429647/6011995
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// configure DI for application services
services.AddScoped<IUserService, UserService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(options =>
options.WithOrigins("http://localhost:8080")
.AllowAnyMethod()
.AllowAnyHeader());
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
}
export const login = (username, password) => {
return axios.request({
url: baseURL + '/api/user/authenticate',
'Content-Type': 'application/json; charset=utf-8',
data: {
'Username': username,
'Password': password
}
})
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)