【问题标题】:Implementing OAuth 2.0 + openID token with ASP.NET5使用 ASP.NET5 实现 OAuth 2.0 + openID 令牌
【发布时间】:2022-01-17 22:38:50
【问题描述】:

我有以下代码在我运行项目时似乎没有做任何事情。 我希望浏览器重定向发生在 http://localhost:5000 请求“代码”授权流。 相反,我只看到“Hello World”。

这是我的 startup.cs 文件:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OAuthService
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = "cookie";
                options.DefaultSignInScheme = "cookie";
                options.DefaultChallengeScheme = "oidc";
            })
            .AddOpenIdConnect("oidc", options =>
            {
                options.Authority = "http://localhost:5000";
                options.RequireHttpsMetadata = false; // dev only

                options.ClientId = "pkce_client";
                options.ClientSecret = "acf2ec6fb01a4b698ba240c2b10a0243";
                options.ResponseType = OpenIdConnectResponseType.Code;
                options.ResponseMode = "form_post";
                options.CallbackPath = "/OAuthService/GetResponse";
                options.UsePkce = true;
            });
        }

        // 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.UseRouting();

            app.UseAuthentication();            

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }
}

【问题讨论】:

    标签: oauth-2.0 openid-connect asp.net5


    【解决方案1】:

    需要有人告诉认证模块用户必须登录。你要么触发它使用

                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapGet("/login", async context =>
                    {
                        var claims = new Claim[]
                        {
                                            //Standard claims
                                            new Claim(ClaimTypes.Name, "Joe Svensson"),
                                            new Claim(ClaimTypes.Country, "Sweden"),
                                            new Claim(ClaimTypes.Email, "joe@edument.se"),
    
                                            //Custom claims
                                            new Claim("JobTitle", "Developer"),
                                            new Claim("JobLevel", "Senior"),
                        };
    
                        ClaimsIdentity identity = new ClaimsIdentity(claims: claims,
                                                          authenticationType: CookieAuthenticationDefaults.AuthenticationScheme);
    
                        ClaimsPrincipal user = new ClaimsPrincipal(identity: identity);
    
                        var authProperties = new AuthenticationProperties
                        {
                            IsPersistent = true
                        };
    
                        //Sign-in the user
                        await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user, authProperties);
    
                        await context.Response.WriteAsync("<!DOCTYPE html><body>");
                        await context.Response.WriteAsync("<h1>Logged in!</h1>");
                    });
    ....
    

    或者您使用添加授权处理程序中间件

        app.UseAuthorization();
    

    并使用以下方法正确配置:

    .AddAuthorization(options =>
    {
        ...
    })
    

    【讨论】:

    • 你的意思是 .AddAuthorization 我想.. 我的代码中已经有 .AddAuthentication
    • 是的,我更新了我的答案,我的答案有意义吗?
    • 好的,我去看看。我的意图是在这里针对第 3 方身份验证服务器执行登录过程。我的实际 API 在另一个项目中使用另一种语言。所以我只需要获取访问令牌、openid 令牌和刷新令牌
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2015-04-01
    • 2020-08-10
    • 2022-09-28
    • 2012-04-17
    • 2014-11-22
    • 2014-08-28
    相关资源
    最近更新 更多