我正在使用ASP.NET WebApi 2應用程序設置Azure API管理。 API Management建議在API管理代理和ASP.NET WebApi之間設置基本身份驗證,以確保只能通過API管理代理訪問WebApi。最簡單的方法將基本身份驗證添加到web.config用戶/傳遞
(當然OAuth憑證將與「真正的」身份驗證請求被髮送依舊,但我會稍後添加。)
考慮到這一點,我真的不希望inplement基本auth在應用程序中,我想通過Web.config專門處理它。
如何設置我的Web.config以使用存儲在Web.config中的用戶名/密碼進行基本身份驗證?
我試圖按照這篇文章(http://www.4guysfromrolla.com/articles/122408-1.aspx),但沒有太多的運氣。
這個問題是沒有答案,但他們可能問類似的事情:web.config single user basic auth我發現這裏包括添加,我不想做應用程序內的認證
其他的答案。
這裏是我的web.config:
<?xml version="1.0"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<appSettings configSource="bin\debug-appSettings.config"/>
<connectionStrings configSource="bin\debug-connectionStrings.config"/>
<system.web>
<compilation targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
<authentication mode="Forms">
<forms>
<credentials passwordFormat="Clear">
<user name="test" password="test" />
</credentials>
</forms>
</authentication>
<authorization>
<allow users="test" />
<deny users="*" />
</authorization>
</system.web>
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
<remove name="OPTIONSVerbHandler"/>
<remove name="TRACEVerbHandler"/>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
<modules>
<remove name="FormsAuthenticationModule" />
<add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />
<remove name="UrlAuthorization" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
</modules>
<security>
<authentication>
<basicAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Azure.AppService.ApiApps.Service" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-0.9.64.0" newVersion="0.9.64.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/>
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="MiniProfiler" publicKeyToken="b44f9351044011a3" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.2.0.157" newVersion="3.2.0.157"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.20622.1351" newVersion="4.0.20622.1351"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.WindowsAzure.Storage" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.1.0.0" newVersion="6.1.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
我發送一個請求與郵差具有以下標題:
Authorization: Basic dGVzdDp0ZXN0
Content-Type: application/json
但我得到的是401.1頁面由IIS。
我缺少什麼?
窗體身份驗證和基本身份認證是兩碼事。如果您使用IIS進行基本身份驗證,則需要使用用戶名和密碼創建一個Windows帳戶。你將需要使用https到你的API。您還可以使用Azure API應用程序並使該API保密。 –
希望這個視頻有點幫助:https://channel9.msdn.com/Blogs/AzureApiMgmt/Last-mile-Security –
** [基本認證](https://en.wikipedia.org/wiki/Basic_access_authentication)* *是一個定義的術語,瀏覽器將彈出一個本機框詢問用戶名和密碼,並且將根據Windows服務器上的Windows用戶帳戶測試給定的憑證。此行爲在https://tools.ietf.org/html/rfc7617中指定。你問的是**基本認證**。 – Gqqnbig