Net 2.1,Angular 7,被CORS策略阻止:在请求的请求中不存在“ Access-Control-Allow-Origin”标头

时间:2018-12-19 18:52:51

标签: asp.net cors

我被CORS政策屏蔽。我已允许在我的startup.cs中访问所有内容。这是我的startup.cs

<form method="post" id="loginForm">
    Username <input action='/userLogin' placeholder="email" class="form-control" name="email" id="email" type="text" required=""> 
        Password <input placeholder="Password" id="password" class="form-control" name="password" type="password">
        <input type="checkbox" id="user" checked="checked"> User 
        <input type="checkbox" id="adminAction"> Admin 
        <a href="#" class="text-right text-white text-capitalize">forgot password?</a>
         <input type="submit" value="Log In"> 
         <a href="/register" class="text-white  font-weight-bold"> Register Now</a>
</form>

<script>
    $(document).ready(function(){
        $('#adminAction').change(function(){
            var checked = $('#adminAction').is(':checked');
            if(checked){
                $('#loginForm').attr('action','/adminLogin');
            }
            else{
                $('#loginForm').attr('action','/userLogin');
            }
        })  
    });
</script>

2 个答案:

答案 0 :(得分:0)

只需在您的ConfigureServices部分中添加以下行,它就可以正常工作:

var corsBuilder = new CorsPolicyBuilder();
        corsBuilder.AllowAnyHeader();
        corsBuilder.WithMethods("GET", "POST");
        corsBuilder.AllowAnyOrigin();
        services.AddCors(options => options.AddPolicy("AllowAll",corsBuilder.Build()));

答案 1 :(得分:0)

请遵循documentation

首先,您需要在ConfigureServices()内启用CORS中间件 其次,您需要告诉应用程序在Configure()

中使用此中间件

示例:

ConfigureServices()中:

    services.AddCors(options =>
    {
        options.AddPolicy("AllowAllOrigins",
            builder =>
            {
                builder
                    .AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod();
            });
    });

Configure()中:

    app.UseCors("AllowAllOrigins");