gpt4 book ai didi

authentication - 为什么/signin-oidc 在使用 Azure AD 登录后返回 404?

转载 作者:行者123 更新时间:2023-12-04 00:59:28 27 4
gpt4 key购买 nike

我正在 ASP.NET Core MVC 3.0 应用程序中实现 Azure AD 身份验证。我已按照 this 中的说明进行操作教程,选项2;唯一的区别是我使用了现有的应用程序而不是可下载的示例(我确保安装所有 NuGet 包并添加教程和示例提供的配置数据)。我正在我的本地机器上测试这个应用程序。

应用程序已在 Azure 中正确注册,因此我有一个客户端 ID 和一个租户 ID。数据如下:

  • 重定向 URI:Web,https://localhost:5002/signin-oidc ;网站,https://localhost:5002 (按此顺序)
  • 登出网址:https://localhost:5002/signout-oidc
  • 隐式授权: ID token

  • 尝试访问标有 [Authorize] 的操作触发登录过程,但当它完成时,它重定向到 https://localhost:5002/signin-oidc然后网站返回 404。
    appsettings.json配置:
    {
    "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "[OMITTED]",
    "TenantId": "[OMITTED]",
    "ClientId": "[OMITTED]",
    "CallbackPath": "/signin-oidc"
    },
    "Logging": {
    "LogLevel": {
    "Default": "Debug",
    "System": "Information",
    "Microsoft": "Information"
    }
    },
    "AllowedHosts": "*"
    }


    启动类:

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

    namespace TestApp.WebSite
    {
    public class Startup
    {
    public Startup(IConfiguration configuration)
    {
    Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
    services.Configure<CookiePolicyOptions>(options =>
    {
    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 += "/v2.0/";
    options.TokenValidationParameters.ValidateIssuer = false;
    });

    services.AddLocalization(options => options.ResourcesPath = "Resources");
    services
    .AddControllersWithViews(options => options.Filters.Add(GetAuthorizeFilter()))
    .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }
    else
    {
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
    }
    app.UseHttpsRedirection();
    app.UseRequestLocalization(GetLocalizationOptions());
    app.UseStaticFiles(GetStaticFileOptions());
    app.UseRouting();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
    endpoints.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
    });
    }

    private RequestLocalizationOptions GetLocalizationOptions()
    {
    var cookie_request_culture_provider = new CookieRequestCultureProvider
    {
    CookieName = "UserCulture"
    };
    var providers = new List<IRequestCultureProvider>()
    {
    cookie_request_culture_provider,
    new AcceptLanguageHeaderRequestCultureProvider()
    };
    var supported_cultures = new CultureInfo[]
    {
    new CultureInfo("en"),
    new CultureInfo("es")
    };
    var result = new RequestLocalizationOptions
    {
    RequestCultureProviders = providers,
    SupportedCultures = supported_cultures,
    SupportedUICultures = supported_cultures,
    DefaultRequestCulture = new RequestCulture("en")
    };
    return result;
    }

    private StaticFileOptions GetStaticFileOptions()
    {
    var result = new StaticFileOptions
    {
    ServeUnknownFileTypes = true,
    DefaultContentType = "text/plain"
    };
    return result;
    }

    private AuthorizeFilter GetAuthorizeFilter()
    {
    var policy = new AuthorizationPolicyBuilder()
    .RequireAuthenticatedUser()
    .Build();
    var result = new AuthorizeFilter(policy);
    return result;
    }
    }
    }


    任何可以为我指明正确方向的建议将不胜感激。

    最佳答案

    那是因为你错过了app.UseAuthentication();以便 OIDC 中间件无法处理您的应用程序中的身份验证过程:

    app.UseAuthentication(); //add this line 
    app.UseAuthorization();

    关于authentication - 为什么/signin-oidc 在使用 Azure AD 登录后返回 404?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59761733/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com