简体   繁体   English

在 .Net Core 中将登录与 Azure Active Directory 集成

[英]Integrating Sign In with Azure Active Directory in .Net Core

I am trying to integrate sign in option through Azure Active Directory with .net core application.我正在尝试通过 Azure Active Directory 与 .net 核心应用程序集成登录选项。 I have been through Microsoft's blogs and I have been to the steps where user can login with Microsoft's account.我浏览过微软的博客,也看过用户可以使用微软帐户登录的步骤。 But the ISSUE is但问题是

"It is always redirecting me to the login action method again." “它总是再次将我重定向到登录操作方法。” which loops me back to sign in screen.这让我回到登录屏幕。

Image for redirect URLS:重定向 URL 的图像: 在此处输入图片说明

My Startup Class我的创业班

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie()
                .AddAzureAD(options => Configuration.Bind("AzureAd", options));
            services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
            {
                options.Authority = options.Authority + "/v2.0/";


                options.TokenValidationParameters.ValidateIssuer = false;

            });

My Login account controller我的登录帐户控制器

[HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> Login(string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
                return RedirectToAction("Index", "Search");

            var domainInfo = await _subdomainProvider.GetDomainInfoAsync(Request.Host.Value);
            if (domainInfo.LoginMethod == LoginMethod.AzureAD)
            {
                var redirectUrl = Url.Action("Index", "Search");
                var properties = _signInManager
                    .ConfigureExternalAuthenticationProperties(LoginMethod.AzureAD.ToString(), redirectUrl);
                return new ChallengeResult(LoginMethod.AzureAD.ToString(), properties);

            }
            else
            {
                ViewBag.ReturnUrl = returnUrl;
                ViewBag.Background = await GetRandomBackgroundAsync(domainInfo.LoginBackgrounds);
            }


            return View(new LoginModel
            {
                Language = GetLanguage(),
                ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList()
            });
        }

Login View登录视图

<form method="post" asp-controller="Account" asp-action="SignIn" asp-route-returnUrl="@Model.ReturnUrl">
        <h2>@AccountStrings.Login_ExternalLogin_Title</h2>
        <div>

            @foreach (var provider in Model.ExternalLogins)
            {
                <button type="submit" class="btn  login-provider @provider.DisplayName"
                        name="provider" value="@provider.Name"
                        title="Log in using your @provider.DisplayName account">
                    @provider.DisplayName
                </button>
            }
        </div>
    </form>

Even added the redirection URL on azure portal to be redirected to action method ie dashboard.but still didn't work.甚至在 azure 门户上添加了重定向 URL 以重定向到操作方法,即仪表板。但仍然不起作用。

Any help would be appreciated.任何帮助,将不胜感激。 Thank you.谢谢你。

According to my understanding, you want to integrate Azure AD with your .net core web application.根据我的理解,您希望将 Azure AD 与您的 .net 核心 Web 应用程序集成。 If so, please refer to the document and the article如果是,请参考文档文章

  1. appsettings.json file appsettings.json 文件
"AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "<you tenant doamin>",
    "TenantId": "<>",
    "ClientId": "<>",
    "CallbackPath": "/signin-oidc"
  1. Add the following code to the startup.cs将以下代码添加到startup.cs
  services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
                .AddAzureAD(options => Configuration.Bind("AzureAd", options));
            //services.Configure<AzureADOptions>(options => Configuration.Bind("AzureAd", options));
            services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
            {
                options.Authority = options.Authority + "/v2.0/";
                options.TokenValidationParameters.ValidateIssuer = false;
            });

That seems you are using ASP.NET Core Identity with Azure AD login .看来您正在使用 ASP.NET Core Identity 和 Azure AD login 。 If that is your scenario , you can set CookieSchemeName to Identity.External so that asp.net core identity can get the external user profile from external identity provider :如果这是您的方案,您可以将CookieSchemeName设置为Identity.External以便 asp.net 核心身份可以从外部身份提供者获取外部用户配置文件:

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(
        Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

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

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

});

Azure AD application setting : Azure AD 应用程序设置:

    "AzureAd": {
        "Instance": "https://login.microsoftonline.com/",
        "Domain": "xxx.onmicrosoft.com",
        "TenantId": "xxxxxx-xxxxx-4f08-b544-b1eb456f228d",
        "ClientId": "xxxxx-xxxxx-4717-9821-e4f718fbece4",
        "CallbackPath": "/signin-oidc",
        "CookieSchemeName": "Identity.External"
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .net core 2.0单一登录与Azure-Active Directory组列表 - .net core 2.0 single sign on with Azure - Active Directory Group Listing 如何使用 Azure Active Directory 单点登录 MFA 身份验证与 dot net core web 应用程序 - How to Use Azure Active Directory Single sign on MFA authentication with dot net core web application Microsoft .net core 2.0 Azure数据湖和Azure Active Directory - Microsoft .net core 2.0 Azure Data Lake and Azure Active Directory Azure Active Directory单一登录 - Azure Active Directory Single Sign On 使用Azure Active Directory单一登录 - Single Sign On with Azure Active Directory 删除具有.net core的Azure Active Directory上要求身份验证的弹出窗口 - Remove authentication required popup on Azure Active directory with .net core .Net Core 2.0 - Azure Active Directory - NGinX反向代理 - HTTPS - .Net Core 2.0 - Azure Active Directory - NGinX reverse Proxy - HTTPS Web API .Net Core Azure Active Directory身份验证 - Web API .Net Core Azure Active Directory Authentication Azure Active Directory -.Net Core 3 Web 应用程序 - 禁用 2 因素身份验证 - Azure Active Directory - .Net Core 3 Web Application - Disable 2 Factor Authentication Azure Active Directory单一登录超时 - Azure Active Directory Single Sign On timeout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM