【问题标题】:Mobile devices with credentials flag to true凭据标记为 true 的移动设备
【发布时间】:2018-12-11 01:01:27
【问题描述】:

我必须发送凭据以在服务器上为我的应用程序进行身份验证(Windows 身份验证):

with-credentials = true

问题是我的客户端是移动设备,我无法告诉 Access-Control-Allow-Origin 中的服务器原始域。

我想这样做:

Access-Control-Allow-Origin = *

但我知道由于安全问题,这是不可能的。

如何使用 HTTP 做到这一点?

PS:我在 ASP.NET 中使用服务器,客户端是用 Ionic (Angular) 制作的。目前,我正在使用临时解决方案:

Access-Control-Allow-Origin = localhost:8100

但是当我部署应用程序时,它无法在真实设备上运行。

【问题讨论】:

  • 那么您是否期望您上线时域的来源会有所不同?就像当你测试清楚为什么你需要它时,因为你使用 localhost,但是在现场场景中 - 你为什么认为你需要 CORS for *?
  • 我不确定它是否适用于真实设备,因为我现在无法对其进行测试(服务器目前仅在我的本地主机上运行)。是的,我认为源域会有所不同,因为它将来自具有 IP 地址的移动设备。此外,如果我的请求是在凭据选项设置为 true 的情况下发送的,我必须使用 CORS,并说明请求的来源。
  • 我在这里找到了解决方案:stackoverflow.com/questions/51384937/…

标签: c# .net angular cors


【解决方案1】:

来自enable-cors.org

ASP.NET 中的CORS

如果您无权配置 IIS,您仍然可以通过 ASP.NET 添加标头,方法是将以下行添加到您的源页面:

Response.AppendHeader("Access-Control-Allow-Origin", "*");

另见:你也可以配置IIS6/IIS7

Reference Taken

【讨论】:

  • 我已经在我们的配置和代码中尝试过了,但是这样做,我得到一个 CORS 错误:Failed to load http://localhost/MyApp/Synchro/Connect: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:8100' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute. 即使我在我的客户端请求中将 with-credentials 设置为 false响应,我仍然收到此错误。
  • 顺便说一下,我可以配置 IIS,但我需要保持 windows 身份验证,我不能精确的来源。也许有 IIS 的解决方案?
  • 引用你需要在 webconfig @bobier2 中添加一些行的链接
  • 我已经有了这些台词! <httpProtocol> <customHeaders> <clear /> <add name="Access-Control-Allow-Origin" value="http://localhost:8100" /> <add name="Access-Control-Allow-Headers" value="Content-Type,Authorization" /> <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" /> <add name="Access-Control-Allow-Credentials" value="true" /> </customHeaders> </httpProtocol>
  • 您在应用程序或网站的根目录中添加了对吗?
【解决方案2】:

有时您需要在 AuthorizeAttribute 中检查这一点

// pre-flight request (OPTIONS) are always ok.
// https://stackoverflow.com/questions/26296779/chrome-v37-38-cors-failing-again-with-401-for-options-pre-flight-requests#28235624
   if (actionContext.Request.Method == System.Net.Http.HttpMethod.Options)
   {
       return true;
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2020-07-05
    • 2010-09-06
    • 2016-08-02
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多