【问题标题】:.net core identity server 4 authentication handler for oidc用于 oidc 的 .net 核心身份服务器 4 身份验证处理程序
【发布时间】:2018-07-30 16:56:49
【问题描述】:

我在进行身份验证时遇到问题。当我尝试连接到身份服务器时,它会引发错误。当我在身份服务器上时,我可以成功登录,但是当我尝试从我的网络应用程序连接到身份服务器时,它会引发以下错误。

谁能看看我做错了什么?

错误:“没有配置身份验证处理程序来处理方案:oidc”

我在我的网站 Startup.cs 中使用以下代码

        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
        services.AddAuthentication(options =>
        {
            options.DefaultScheme =
                CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme =
                OpenIdConnectDefaults.AuthenticationScheme;
        })
        .AddCookie()
        .AddOpenIdConnect(options =>
        {
            options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.Authority = "http://localhost:5000"; // Auth Server
            options.RequireHttpsMetadata = false; // only for development 
            options.ClientId = "mvc"; // client setup in Auth Server
            options.ClientSecret = Configuration["Identity_Server:Client_Secret"].Sha256();
            options.ResponseType = "code id_token"; // means Hybrid flow
            options.Scope.Add("API1");
            options.GetClaimsFromUserInfoEndpoint = true;
            options.SaveTokens = true;
        });

        services.AddMvc();

我在 Identity Startup.cs 中使用以下内容

        services.AddDbContext<DbContext>(options =>
            options.UseMySQL(Configuration.GetConnectionString("MySQL")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<DbContext>()
                .AddDefaultTokenProviders();

        // Add application services.
        services.AddTransient<IEmailSender, EmailSender>();

        var config = new Config(Configuration);
        services.AddIdentityServer()
                .AddDeveloperSigningCredential(filename: "tempkey.rsa")
                .AddInMemoryIdentityResources(config.GetIdentityResources())
                .AddInMemoryApiResources(config.GetApiResources())
                .AddInMemoryClients(config.GetClients())
                .AddAspNetIdentity<ApplicationUser>();

        services.AddMvc();

我在我的配置文件中使用以下内容

    private static IConfiguration _config;

    public Config(IConfiguration configuration)
    {
        _config = configuration;
    }

    public IEnumerable<Client> GetClients()
    {
        return new List<Client>
        {
            new Client
            {
                ClientId = "mvc",
                ClientName = "MVC Client",
                AllowedGrantTypes = GrantTypes.Hybrid,
                RequireConsent = false,
                ClientSecrets =
                {
                    new Secret(_config["secret"].Sha256())
                },
                RedirectUris           = { "http://localhost:5002/signin-oidc" },
                PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },
                AllowedScopes =
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    "API1"
                },
                AllowOfflineAccess = true
            }
        };
    }

    public  IEnumerable<IdentityResource> GetIdentityResources()
    {
        return new List<IdentityResource>
        {
            new IdentityResources.OpenId(),
            new IdentityResources.Profile(),
        };
    }

    public IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource>()
        {
            new ApiResource("API1", "Allow to Manage API1")
        };
    }

【问题讨论】:

    标签: asp.net-core-mvc identityserver4 openid-connect


    【解决方案1】:

    您需要这样定义的名称、挑战和处理程序:

    public void ConfigureServices(IServiceCollection services)
    {
      services.AddMvc();
    
      JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
    
      services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Cookies";
            options.DefaultChallengeScheme = "oidc";
        })
        .AddCookie("Cookies")
        .AddOpenIdConnect("oidc", options =>
        {
            options.SignInScheme = "Cookies";
    
            options.Authority = "http://localhost:5000";
            options.RequireHttpsMetadata = false;
    
            options.ClientId = "mvc";
            options.SaveTokens = true;
        });
    

    }

    http://docs.identityserver.io/en/release/quickstarts/3_interactive_login.html

    【讨论】:

    • 想知道您是否可以帮助解决以下错误,错误:“消息包含错误:'Invalid_client',error_description:'error_description is null',error_uri:'error_uri is null'”
    • @Max 嗯,不确定。在文档中,在身份服务器之前添加了 mvc,这有帮助吗?
    • 重定向似乎有问题
    • 恐怕我现在无能为力:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 2019-02-23
    • 1970-01-01
    相关资源
    最近更新 更多