![](/img/trans.png)
[英]Web application with Azure Active Directory always redirects to the '~/.auth/login/done' URL
[英]Azure Active Directory always redirects to '~/.auth/login/done' when deployed to Azure despite working on localhost
因此,我正在開發一個ASP.NET Core應用程序(.NET Core 2.0),該應用程序在Azure上作為應用程序服務托管。 我想使用一個租戶(因此僅來自我們公司的帳戶)實現與Azure AD的身份驗證。 實際上,我添加了所有必要的代碼,注冊了應用程序,並在App Service中配置了所有內容(至少我認為是這樣),並且即使在本地運行它也可以正常工作。 當我將應用程序發布到Azure並嘗試在那里登錄時,會發生問題。 我沒有被重定向到我選擇的視圖,而是被重定向到了“〜/ .auth / login / done”,並且看到了以下內容: https ://imgur.com/a/6OeKUNy
因此,正如我所說,我注冊了該應用程序並添加了應用程序URL和回復URL,它們看起來像這樣:
我在“身份驗證/授權”部分使用高級設置配置了應用程序服務本身。 當前,“客戶機密”和“允許的令牌受眾”字段為空。 我不希望用戶在進入網站后立即獲得身份驗證,但是當他單擊某個按鈕時,我設置為“允許匿名請求(不執行任何操作)”。 我添加了必要的代碼以使其正常工作:
AccountController:
[Authorize]
[Route("[controller]/[action]")]
public class AccountController : Controller
{
private readonly OmsIntegrationContext _context;
private readonly IUserService _userService;
public AccountController(OmsIntegrationContext context, IUserService userservice)
{
_context = context;
_userService = userservice;
}
[HttpGet]
[AllowAnonymous]
public IActionResult LoginAzureAd(string returnUrl)
{
var redirectUrl = Url.Action(nameof(ChangeRequestController.Index), "ChangeRequest");
return Challenge(new AuthenticationProperties { RedirectUri = redirectUrl },
OpenIdConnectDefaults.AuthenticationScheme);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task LogoutAzureAd()
{
if (User.Identity.IsAuthenticated)
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
}
}
Startup.cs:
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
x.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
x.DefaultSignOutScheme = CookieAuthenticationDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddOpenIdConnect(options =>
{
var azureAdConfig = Configuration.GetSection("AzureAd");
options.Authority = azureAdConfig.GetValue<string>("Instance") +
azureAdConfig.GetValue<string>("Domain");
options.ClientId = azureAdConfig.GetValue<string>("ClientId");
options.ResponseType = OpenIdConnectResponseType.IdToken;
options.CallbackPath = azureAdConfig.GetValue<string>("Callback");
options.SignedOutRedirectUri = "https://appname.azurewebsites.net/";
options.TokenValidationParameters.NameClaimType = "name";
options.UseTokenLifetime = true;
})
.AddCookie()
.AddSalesforceAuthentications(AuthConfigs);
services.ConfigureApplicationCookie(x =>
{
x.Cookie.Name = ".SalesForce.Cookie"; //This isn't my code. Could this cause problems?
});
注意:通常在此項目中使用salesforce auth,對於沒有SF帳戶的用戶,Azure AD應該僅對特定模塊起作用。 這不是我的主意,但我必須這樣做
appsettings.json:
"AzureAd": {
"ClientId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"TenantId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Instance": "https://login.microsoftonline.com/",
"Domain": "XXXXX.onmicrosoft.com",
"Callback": "/.auth/login/aad/callback"
}
我要實現的目標以及在本地主機上運行時要實現的目標是,當用戶進入Web應用程序時,他單擊一個按鈕,並使用公司帳戶通過Azure AD進行身份驗證,然后重定向到〜/ ChangeRequest / Index。 但是部署后我無法使其正常工作。 我在這里發現了類似的問題:
但是這個人解決問題的方式對我來說不是一個選擇。 有任何想法嗎?
我設法解決了這個問題。 顯然,一個人無法同時在App Service和一個人的代碼中配置此身份驗證。 我要做的就是在App Service的“身份驗證/授權”部分中關閉Azure Portal上的身份驗證。 這是我關於stackoverflow的第一個問題,我真的不知道是否應該刪除此問題。 如果是這樣,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.