VB ASP.Net Windows身份验证不适用于Angular

时间:2019-05-16 17:21:07

标签: asp.net angular vb.net cors windows-authentication

问题

我正在尝试设置CORS策略,以便我的Angular应用可以调用ASP.net Web服务。我可以对两个应用程序进行更改。

我在Chrome中收到此错误:

  

在以下位置访问XMLHttpRequest   'http://localhost:11434/myurl/mywebservice.asmx'   来自来源“ http://localhost:4200”的信息已被CORS政策阻止:   对预检请求的响应未通过访问控制检查:   响应中“ Access-Control-Allow-Credentials”标头的值   为”,当请求的凭据模式为时,必须为“ true”   '包括'。发起的请求的凭据模式   XMLHttpRequest由withCredentials属性控制。

Edge中的这个:

  

HTTP401:拒绝-请求的资源需要用户身份验证。   (XHR)选项-   http://localhost:11434/myurl/mywebservice.asmx

我能够在邮递员中成功发出请求。

响应头

  

访问控制允许来源→{http://localhost:4200

     

访问控制允许方法→GET,PUT,POST,DELETE,HEAD,OPTIONS

     

访问控制权限凭证→true

角度

我正在使用拦截器将标头添加到每个请求中。

AuthInterceptor

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  constructor(){}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    req = req.clone({
        withCredentials: true
    });

    console.log(req);

    return next.handle(req);
  }
}

ApiService

public getData() {
    const url: string = myBackendUrl;
    const requestData = {
      data: "my request data"
    }

    return this.http.post(url, requestData, {responseType: 'json'});
  }

AppModule

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule
  ],
  providers: [
    ApiService,
    {provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true}
  ],
  bootstrap: [AppComponent]
})

ASP.Net

使用web.config设置CORS。

web.config

<system.web>
  <authentication mode="Windows"/>
  <authorization>
    <allow users="?"/>
  </authorization>
</system.web>

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
        <add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, HEAD, OPTIONS" />
        <add name="Access-Contol-Allow-Credentials" value="true" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

我尝试过的

我浏览了以下帖子以尝试解决此问题。

'Access-Control-Allow-Credentials' header in the response is '' which must be 'true'

Access-Control-Allow-Credentials' header in the response is "" which must be 'true'

he value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true'

CORS: credentials mode is 'include'

Credentials flag is 'true', but the 'Access-Control-Allow-Credentials

Windows Authentication and Angular 4 application

谢谢您的帮助。

0 个答案:

没有答案