Azure Active Directory ASP. NET Core 3.1 "Мы не можем войти в систему. Пожалуйста, попробуйте еще раз." Мы пропускаем какие-либо конфигурации? - PullRequest
0 голосов
/ 07 марта 2020

Мы используем шаблоны NET core 3.1, Azure AD и Visual Studio Connected Services, которые пытаются сгенерировать файл startup.cs.

Не настраиваем ли мы что-то в конфигурации? Мы не используем секрет клиента, но может быть в этом проблема? Подключенные службы сгенерировали один.

Startup.cs

 public class Startup
    {

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
                    .AddAzureAD(options => Configuration.Bind("AzureAd", options));

            services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
            {
                options.Authority = options.Authority + "/v2.0/";         // Microsoft identity platform

                options.TokenValidationParameters.ValidateIssuer = false; // accept several tenants (here simplified)
            });



            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {


            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();
            app.UseAuthentication();
            //The new UseEndpoints method
            app.UseEndpoints(endpoints =>
            {             
                endpoints.MapControllers();
            });
        }
    }
}

AppSettings. json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "AzureAd": {
    "ClientId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "Domain": "macrowhq.onmicrosoft.com",
    "Instance": "https://login.microsoftonline.com/",
    "TenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "CallbackPath": "/signin-oidc",
    "ClientSecret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "AppIDURL": "https://xxxxxxxx.onmicrosoft.com/WebApplication11",
    "ConfigView": "MVC"
  }
}

Настройки запуска. json

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:61517",
      "sslPort": 44363
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "https://localhost:44363/weatherforecast",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApplication11": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

500 при входе oid c enter image description here

Мы увидели экран входа в наш домен

Ошибка «Мы не можем войти в систему. Пожалуйста, повторите попытку». произошло только после того, как мы ввели ID пользователя / пароль (да, мы уверены, что он был верным.)

Странно то, что учетная запись пользователя отображается, несмотря на это сообщение.

enter image description here

Request URL: https://localhost:44363/signin-oidc
Request Method: POST
Status Code: 500 
Remote Address: [::1]:44363
Referrer Policy: strict-origin-when-cross-origin
date: Fri, 06 Mar 2020 21:56:30 GMT
server: Microsoft-IIS/10.0
status: 500
x-powered-by: ASP.NET
:authority: localhost:44363
:method: POST
:path: /signin-oidc
:scheme: https
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
cache-control: no-cache
content-length: 1681
content-type: application/x-www-form-urlencoded
cookie: .AspNetCore.OpenIdConnect.Nonce.CfDJ8Lp55qWi8TlPh0rBkxAxUdnRkP-8BQvFEB24nS5JppFP4F8...
        .AspNetCore.Correlation.AzureADOpenID.SGSLll-jiFohbpdZx0poFsgBfko79OWPcNnHcA7gEXs=N...
        .AspNetCore.OpenIdConnect.Nonce.CfDJ8Lp55qWi8TlPh0rBkxAxUdnofEIcTW_vXX1wIOPuDZ5u_dF...
        .AspNetCore.Correlation.AzureADOpenID.2eJUPEWi45vkhK12OGyyEGfTVDnHQj-ydAmkECan5ZE=N...
        .AspNetCore.AzureADCookie=CfDJ8Lp55qWi8TlPh0rBkxAxUdlMAHAFXphv1Kp9khZBbtL5kMZf6lLLU...

origin: https://login.microsoftonline.com
pragma: no-cache
referer: https://login.microsoftonline.com/
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: cross-site
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36
id_token: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IkhsQzBSMTJza3hOWjFXKdKmhGqmuL0CnCHYkt8QtFdlCQfsjGeQZ...
state: CfDJ8Lp55qWi8TlPh0rBkxAxUdnJ07dNioFne0krEAmVmwXBA-5pCOswFvE6Kt0oIPcckWGfljAPW3b8XnTXCY5-dGLhxrS8fI0A...
session_state: b4d30d59-0e31-4645-9876-e002bd1c7...

Ответы [ 2 ]

1 голос
/ 09 марта 2020

Пожалуйста, обратите внимание на ваш заказ промежуточного программного обеспечения. Изменить:

app.UseAuthorization();
app.UseAuthentication();

Кому:

app.UseAuthentication();
app.UseAuthorization();

, чтобы промежуточное ПО аутентификации обрабатывало ответ AAD и аутентифицировало пользователя.

0 голосов
/ 10 марта 2020

Сейчас нет примеров для Core 3.1 Web API, который мы используем для решения Angular / Web API. Вот как заставить его работать.

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.AzureAD.UI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;

namespace Core31API
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
                .AddAzureAD(options => Configuration.Bind("AzureAd", options));

            services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
            {
                options.Authority = options.Authority + "/v2.0/";


            });
        }


        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

appsettings. json

{
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "xxx.onmicrosoft.com",
    "TenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "ClientId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Свойства / launchSettings. json

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:45978",
      "sslPort": 44362
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Core31API": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Контроллер WeatherForeCast.cs ...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Core31API.Controllers
{
    [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

При первом запуске приложения вам будет предложено войти в систему, затем попросил предоставить разрешения. Нажмите да ко всему, и вы должны увидеть прогноз погоды.

Если вы отслеживаете сетевой обмен после входа в свой домен с правильным идентификатором пользователя / паролем. Вы должны увидеть этот обмен в Браузере.

cookie exchange

Набор для установки повара ie, идентификатор входа c это перенаправление, которое просит сервер перенаправить на защищенный порт SSL 443.

Трассировка Wireshark

tls

Обратите внимание, что весь обмен пакетами TLS осуществляется через порт SSL 443 на локальном компьютере. Это не дает хакерам увидеть обмен из браузера, вам нужно запустить Wireshark, чтобы увидеть его. Большинство компаний не разрешают установку Wireshark для не-разработчиков.

ПРИМЕЧАНИЕ. Несмотря на то, что это работало из Backend, нам не удалось получить интерфейс для использования этой конечной точки Web.API. Я задал этот вопрос для этой проблемы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...