c - 欧文 : webapi call returning 401 unauthoried Error in Azure

标签 c azure asp.net-web-api forms-authentication basic-authentication

我创建了两个应用程序,一个用于前端(使用 AngularJs 和 Asp.Net MVC 创建),另一个用于后端,我使用 Web Api 应用程序。

我正在尝试在前端使用表单例份验证来保护我的应用程序,并在后端实现基本表单例份验证。

当我在 IIS 中进行本地测试时,它工作正常。但是当我在 Azure 中托管应用程序时,带有身份验证属性的 API 调用会返回 401 未经授权的错误。

响应标题:

 Access-Control-Allow-Origin:*
    Cache-Control:no-cache
    Content-Length:0
    Date:Wed, 05 Oct 2016 11:08:30 GMT
    Expires:-1
    Pragma:no-cache
    Server:Microsoft-IIS/8.0
    Set-Cookie:ARRAffinity=925cd503f9e5771e9ad651c59baa4a1f678d93cac8b9da4dc9bcb64892ac8710;Path=/;Domain=FrontEnd.azurewebsites.net
    WWW-Authenticate:Basic realm="Backend.azurewebsites.net"
    X-AspNet-Version:4.0.30319
    X-Powered-By:ASP.NET

请求 header ;

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:backend.azurewebsites.net
Origin:http://FrontEnd.azurewebsites.net
Pragma:no-cache
Referer:http://FrontEnd.azurewebsites.net/
Token:null
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36

但是在本地以及请求 header

    Authorization: Basic dmlzaG53guFua2FyLnNha3Roadh4bEBnbWFpbC5jb206MTIzMTIz
   Cookie: __RequestVerificationToken_L3Nfg4dwc3Bh0=YGwlXkckjhg6xTESkI-lN9sNtDVsfefshsrvGWPKekmv_GLZ0Rq8eA5P8Z7sdf4trwbPX4WymcGJJrLamQLMe2Vf2D2fVUg4qJptOvcsCWg1; ASP.NET_SessionId=gk4s1rlsfg5gfeafo3455dkr; .ASPXAUTH=C8113FD23FC4902dsfrsdf5FB3399BA121703F41F9A57AEA50667F2AD4FC02F3D3955215A91F6A3A2EA4287202522380435A93D8BE78D45E6D24FB7E05F64653AE06E0026DEB2911343591563F70EE8EE76338F07CBABE6EA4B95359F3C31sdf9C999sdf2864849gfdB916A06gfds29E3C8D01D2371807E98335000D4D14B391CCE3zxcd49DB77696F4BB77E4434

已通过

授权和 Cookie 参数未添加到来自具有表单例份验证的 Azure 前端应用程序的请求中。

我在 azure 中查看了我的后端应用程序的身份验证/授权配置。任何人都可以知道问题是什么或我需要在 azure 门户中配置什么。提前致谢。

最佳答案

这个问题是由于cors造成的 您可以通过添加您的请求来解决此问题,

 $http({
  ....
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  withCredentials: true,
  })

Web API 的WEB.Config

 <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
            <handlers>
                <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
                <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
                <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
                <remove name="OPTIONSVerbHandler" />
                <remove name="TRACEVerbHandler" />

                <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
                <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
                <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
            </handlers>


            <httpProtocol>
                <customHeaders>
                    <add name="Access-Control-Allow-Origin" value="**FrontEnd URL**" />
                    <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, Cache-Control" />
                    <add name="Access-Control-Allow-Credentials" value="true" />
                    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
                </customHeaders>
            </httpProtocol>
      </system.webServer>

您还引用了这个 URl: https://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

关于c - 欧文 : webapi call returning 401 unauthoried Error in Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39873607/

相关文章:

通过 web api 下载 Excel 文件。变得腐败

c++ - 无法将不完整类型 'void *' 的参数转换为 'const bitData'

c - 数据结构和患者记录

mysql - 如何使用 C# 连接到 Azure MySQL In App localdb

c# - Angular - 向 Owin+Web API Controller 发出带有参数的 HTTP GET 请求未找到

c# - HandleErrorAttribute 的派生类是不是捕获异常?

c - 如何欺骗预处理器

arduino - Arduino Ethernet 的库 IPAddress() 有什么好处?

python - 使用 python 更快地搜索 Azure blob 名称?

azure - 如何允许 GZIP 用于 Azure 表存储查询