简体   繁体   English

将Asp.net核心控制器限制为特定域名

[英]limit Asp.net core controller to specific domain name

I have a web API controller in asp.net core version 2. 我在asp.net核心版本2中有一个Web API控制器。
this controller must receive requests from one domain for example test.com 该控制器必须从一个域接收请求,例如test.com
test.com 's IP always change, therefor i can't limit this controller to IP. test.com的IP总是在变化,因此我不能将此控制器限制为IP。
what method can i use ? 我可以使用什么方法?
just one of the controllers and the other's allow's any origins. 只是其中一个控制器,另一个是允许的任何起源。
the controller is like this : 控制器是这样的:

    [HttpPost]
    public IActionResult CBack([RequiredFromQuery]string id,
                               [FromBody] Newtonsoft.Json.Linq.JObject Cypher)

You can use scoped Cross-Origin Requests (CORS) : 您可以使用范围跨域请求(CORS)

[EnableCors("YourPolicy")]
[HttpPost]
public IActionResult CBack([RequiredFromQuery]string id,
                           [FromBody] Newtonsoft.Json.Linq.JObject Cypher)

This allows you to enable Cross-Origin Requests per controller or action. 这使您可以针对每个控制器或操作启用跨域请求。

Don't forget to add the CORS service in your Startup.cs file. 不要忘记在您的Startup.cs文件中添加 CORS服务。 You must also register a custom policy first. 您还必须首先注册自定义策略。

// ConfigureServices
services.AddCors(options =>
{
    options.AddPolicy("YourPolicy",
        builder => builder.WithOrigins("http://example.com"));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM