![](/img/trans.png)
[英]How to read the Azure App Configuration in Service Fabric ASP.NET Core Stateless Web API?
[英]How to configure .NET 7 Web API for deploying to Azure App service
我創建了一個 Azure .NET 7 樣本 Web API 並想將其發布到我的 Azure 應用程序服務 - 配置后,然后在 Visual Studio 中選擇發布按鈕,過了一會兒我在 88341325048392487 中獲得了成功信息 之后,瀏覽器用我的網站地址打開,但返回未找到 404 錯誤
我已經添加了 Microsoft Identity 平台(以前的 Azure AD)- 也許這會以某種方式影響這一點,但我使用相同的 Azure AD 用於我的其他 Azure API 沒有問題。
當我在本地運行時,這個 API 被啟動並且頁面在瀏覽器中成功打開。
appsettings.json
的內容
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "....onmicrosoft.com",
"TenantId": "...",
"ClientId": "...",
"CallbackPath": "/signin-oidc",
"Scopes": "access_as_user"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"MicrosoftGraph": {
"BaseUrl": "https://graph.microsoft.com/v1.0",
"Scopes": "user.read"
}
}
我按照 Visual Studio Web API 創建向導中的步驟進行了發布 window 連接驗證成功
還檢查了這個快速入門指南- 沒有注意到我可能做錯了什么,但它適用於 .NET 6,而不是 .NET 7,所以也許 .NET 7 需要做一些不同的事情
它應該重定向到https://login.microsoftonline.com/..。 授權,但它只是返回未找到
從這個stackoverflow 帖子發現我需要為我的 API 手動創建一個 web.config,創建它到此為止,但似乎仍然缺少一些東西
<?xml version="1.0" encoding="utf-8"?> <configuration> <location path="." inheritInChildApplications="false"> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="do.net" arguments=".\MyWebApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" /> </system.webServer> <system.web> <httpRuntime targetFramework=.net6.0" /> </system.web> </location> </configuration>
更新
事實證明它在本地也不起作用- 當通過 swagger 嘗試端點時它返回 401 www-authenticate bearer
更新2
當我創建 Web API 我選擇使用Microsoft Identity platform
並生成此代碼。 我需要在這里配置 AddJwtBearer 嗎?
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd")).EnableTokenAcquisitionToCallDownstreamApi().AddMicrosoftGraph(builder.Configuration.GetSection("MicrosoftGraph")).AddInMemoryTokenCaches();
當我們創建 Web API 項目,並在本地運行它時,它會重定向到 Swagger UI 列出所有 API 並幫助測試 api,但並不意味着 Swagger 頁面是默認網站,它只會出現開發環境下,發布到Azure web app后,API首頁會是xx.azurewebsite.net
但是沒有API或者View綁定到這個路由,所以404錯誤說明應用中沒有API路由xx.azurewebsite.net
。 通常情況下,API 路由應該類似於xx.azurewebsite.net/api/controller/xx
如果你想讓你的web API受到Azure AD的保護,可以看看這個文檔。 這里總結一下,需要在Program.cs
中添加Cors策略和Azure廣告認證。
using Microsoft.Identity.Web;
builder.Services.AddCors(options =>
{
options.AddPolicy(name: "mypolicy",
policy =>
{
policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
});
});
builder.Services.AddMicrosoftIdentityWebApiAuthentication(builder.Configuration);
...
app.UseCors("mypolicy");
app.UseAuthentication();
app.UseAuthorization();
然后這里用到了一些配置,需要在appsettings.json
中添加配置。 屬性值你可以按照文檔創建Azure AD資源獲取。
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"ClientId": "azure_ad_app_id",
"ClientSecret": "client_secret",
"Domain": "tenant_id",
"TenantId": "tenant_id"
//I used the same azure application which exposed api and add api permission,so I can comment this line
//"Audience": "api://azure_ad_app_id_which_exposed_api"
},
最后,在Controller中,在controller之前添加[Authorize]
。那么api現在被Azure AD保護,你必須有正確的訪問令牌才能訪問它。
假設您公開委托 API權限。 然后,您分配給 Azure AD 應用程序的 api 權限應類似於api://azure_ad_app_id_exposed_api/permission_name_you_defined
。 然后您可以使用以下鏈接使用授權代碼流來生成訪問令牌。
https://login.microsoftonline.com/tenant_id/oauth2/v2.0/authorize?
client_id=azure_ad_app_id
&response_type=code
&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
&response_mode=query
&scope=profile email openid offline_access
&state=12345
訪問上面的URL會要求你用你的微軟賬號登錄,登錄成功后你會在瀏覽器中看到URL就像復制代碼的值,接下來需要使用它。
http://localhost/myapp/?
code=0.ASxxx
&state=12345&session_state=c71be61a-c4cc-4624-9fa4-fe6c4b2d6b88
然后使用如下請求生成訪問令牌:
Post: https://login.microsoftonline.com/tenant_id/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded
client_id=azure_ad_app_id
&scope=api://azure_ad_app_id_exposed_api/api_name
&code=O.xxxx
&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
&grant_type=authorization_code
&client_secret=client_secret
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.