小编xyn*_*cat的帖子

用于PUT和POST的Angular4 ASP.NET Core 1.2 Windows身份验证CORS提供401

我的IDE是Visual Studio 2017.我有一个Angular4客户端与Core中的WebAPI后端通信,而CORS正在为PUT和POST方法配置EXCEPT.GET方法在Chrome中使用与PUT和POST方法相同的预检OPTIONS方法,但GET工作正常.

Visual Studio中的IIS Express服务器似乎没有将请求转发到Kestrel服务器.两种方法都适用于Postman,但是当Angular4进行调用时则不行.这是代码:

Angular4 POST

post(api: string, object: any): Observable<any> {
        let body = JSON.stringify(object);

        let options = new RequestOptions({
            headers: this.headers,
            withCredentials: true
            });

        return this.http.post(this.server + api, body, options)
            .map((res: Response) => res.json())
            .catch((error: any) => Observable.throw(error.json().error) || 'Post server error');
    }
Run Code Online (Sandbox Code Playgroud)

Startup.cs配置

services.Configure<IISOptions>(options => 
     options.ForwardWindowsAuthentication = true);

services.AddCors(options => {
            options.AddPolicy("AllowAll", builder => {
                builder.WithOrigins("http://localhost:XXXX")
                .WithMethods("GE??T", "POST", "PUT", "DELETE", "OPTIONS")
                .WithHeaders("Origin", "X-Requested-With", "Content-Type", "Accept", "Authorization")
                .AllowCredentials();
            });
        });
Run Code Online (Sandbox Code Playgroud)

Startup.cs ConfigureServices

app.UseCors("AllowAll");
Run Code Online (Sandbox Code Playgroud)

Project中的IIS ApplicationHost.Config

<anonymousAuthentication …
Run Code Online (Sandbox Code Playgroud)

.net iis asp.net-core-webapi angular

8
推荐指数
3
解决办法
5590
查看次数

标签 统计

.net ×1

angular ×1

asp.net-core-webapi ×1

iis ×1