因此,我正在开发一个 ASP.NET Core 应用程序 (.NET Core 2.0),作为 Azure 上的应用服务托管。我想使用单个租户(因此只有我们公司的帐户)通过 Azure AD 实施身份验证。实际上,我添加了所有必要的代码,注册了应用程序并在应用程序服务中配置了所有内容(至少我是这么认为),当我在本地运行它时,它甚至可以正常工作。当我将应用程序发布到 Azure 并尝试登录时出现问题。我没有被重定向到我选择的视图,而是被重定向到“~/.auth/login/done”,我看到了:https://i.stack.imgur.com/gU3Vf.jpg https://i.stack.imgur.com/gU3Vf.jpg
正如我所说,我注册了应用程序并添加了应用程序 url 和回复 url,它们如下所示:
- [应用程序名称].azurewebsites.net/.auth/login/aad/callback
- https://localhost:44359/.auth/login/aad/callback https://localhost:44359/.auth/login/aad/callback
我在“身份验证/授权”部分中使用高级设置配置了应用程序服务本身。目前,“客户端密钥”和“允许的令牌受众”字段为空。
我不希望用户在进入网站后立即进行身份验证,而是在他单击某个按钮时进行身份验证,因此我设置了“允许匿名请求(无操作)”。
我添加了必要的代码以使其工作:
账户控制器:
[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);
}
}
启动.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,Azure AD 应该仅对某个模块起作用,对于没有 SF 帐户的用户。这不是我的主意,但我必须这样做
应用程序设置.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。但部署后我无法使其工作。我在这里发现了类似的问题:
https://forums.asp.net/t/2155544.aspx?AAD+REPLY+URL+issue+signin+oidc+vs+auth+login+aad+callback+Azure+政府+ https://forums.asp.net/t/2155544.aspx?AAD+REPLY+URL+issue+signin+oidc+vs+auth+login+aad+callback+Azure+Government+
但这个人解决问题的方式不适合我。有任何想法吗?