c# - 尽管在本地主机上工作,部署到 Azure 时 Azure Active Directory 始终重定向到 '~/.auth/login/done'

标签 c# azure asp.net-core azure-active-directory azure-web-app-service

因此,我正在开发一个 ASP.NET Core 应用程序 (.NET Core 2.0),作为应用服务托管在 Azure 上。我想使用单个租户(因此只有我们公司的帐户)通过 Azure AD 实现身份验证。实际上,我添加了所有必要的代码,注册了应用程序并在应用程序服务中配置了所有内容(至少我是这么认为),当我在本地运行它时,它甚至可以正常工作。当我将应用程序发布到 Azure 并尝试登录时出现问题。我没有被重定向到我选择的 View ,而是被重定向到“~/.auth/login/done”,我看到了: /image/gU3Vf.jpg

正如我所说,我注册了应用程序并添加了应用程序网址和回复网址,它们如下所示:

我在“身份验证/授权”部分中使用高级设置配置了应用服务本身。目前,“客户端 key ”和“允许的 token 受众”字段为空。 我不希望用户在进入网站后立即进行身份验证,而是在他单击某个按钮时进行身份验证,因此我设置了“允许匿名请求(无操作)”。 我添加了必要的代码以使其正常工作:

帐户 Controller :

    [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 帐户的用户。这不是我的主意,但我必须这样做

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。但部署后我无法使其工作。我在这里发现了类似的问题:

https://forums.asp.net/t/2155544.aspx?AAD+REPLY+URL+issue+signin+oidc+vs+auth+login+aad+callback+Azure+Government+

但是这个人解决问题的方式不适合我。有什么想法吗?

最佳答案

我设法解决了这个问题。显然,人们无法同时在应用服务和代码中配置此身份验证。我所要做的就是在应用服务的“身份验证/授权”部分中关闭 Azure 门户上的身份验证。 这是我在 stackoverflow 上的第一个问题,我真的不知道是否应该删除这个问题。如果是这样,请告诉我。

关于c# - 尽管在本地主机上工作,部署到 Azure 时 Azure Active Directory 始终重定向到 '~/.auth/login/done',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57477564/

相关文章:

c# - 数据报包等效项

c# - XNA:如何获得 BackBuffer RenderTarget

linux - SonarQube 不延长超时

azure - 无法使用 Spring.NET 将依赖项注入(inject)到 Azure WorkerRole 对象

c# - 如何使用类作为属性

c# - 将扩展方法传递给需要委托(delegate)的方法。这是如何运作的?

azure - 将 AbortOnConnectFail 设置为 false 后,我们仍然会在 Azure REDIS 上收到 ConnectTimeout

asp.net-core - 带有 docker-compose 的 Identity Server 4 在/connect/authorize/callback 之后重定向到登录页面

asp.net-core - dnx451 实际上意味着 .net 4.5

c# - .NET 4.6 上的 ASP.NET Core - 如何执行 https