现有Asp.Net核心应用程序中缺少与Azure Active Directory连接的服务的身份验证

时间:2019-02-05 19:03:48

标签: c# azure asp.net-core azure-active-directory

我有现有的Asp.net core 2.0应用程序。我正在尝试向其添加使用Azure Active Directory连接的服务的身份验证。当我尝试右键单击连接的服务并检查与Azure Active Directory连接的服务进行身份验证时,找不到该选项。我在线搜索,发现对于现有的asp.net核心应用程序,没有连接的服务选项。在这种情况下将如何解决?有什么提示吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试以下步骤:

  1. 安装软件包:Microsoft.AspNetCore.Authentication.AzureAD.UI

  2. 修改Startup.cs以启用Azure AD身份验证:

    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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    
  3. 添加身份验证中间件以配置:

    app.UseAuthentication();
    
  4. 修改appsettings.json以添加Azure AD应用设置

    {
    "AzureAd": {
        "Instance": "https://login.microsoftonline.com/",
        "Domain": "xxxxxxx.onmicrosoft.com",
        "TenantId": "xxxxxx-e83b-4099-93c2-8ae86358d05c",
        "ClientId": "xxxxxxxx-80c5-4bd4-ad6a-a967ea0066d6",
        "CallbackPath": "/signin-oidc"
    },
    "Logging": {
        "LogLevel": {
        "Default": "Warning"
        }
    },
    "AllowedHosts": "*"
    }
    

另一种方法是手动配置OpenId Connect中间件,您可以参考以下文章:

https://joonasw.net/view/aspnet-core-2-azure-ad-authentication