APIOAuth2TokenManagementwithIdentityModel.AspNetCoreforAuthorizationCodeTokenRequest
创始人
2024-09-08 00:01:34
0

API OAuth2 Token Management是一种重要的方案,它使用IdentityModel.AspNetCore实现AuthorizationCodeTokenRequest。该解决方法需要遵循以下步骤:

  1. 在NuGet中安装IdentityModel.AspNetCore。

  2. 添加以下代码段到Startup类的ConfigureServices方法中,以便设置IdentityServer4作为身份提供者。

using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System.IdentityModel.Tokens.Jwt; using IdentityModel.AspNetCore.OAuth2Introspection; using IdentityModel;

public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

    services.AddAuthentication(options =>
    {
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    })
        .AddCookie()
        .AddOpenIdConnect(options =>
        {
            options.Authority = "https://demo.identityserver.io";
            options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;

            options.ClientId = "interactive";
            options.ResponseType = OidcConstants.ResponseTypes.Code;
            options.Scope.Clear();
            options.Scope.Add("openid");
            options.Scope.Add("profile");
            options.Scope.Add("email");

            options.SaveTokens = true;
            options.GetClaimsFromUserInfoEndpoint = true;
        });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseAuthentication();
    app.UseMvc();
}

}

  1. 然后,即可使用AuthorizationCodeTokenRequest将OAuth2令牌交换为访问令牌:

var client = new HttpClient();

var disco = await client.GetDiscoveryDocumentAsync("https://demo.identityserver.io"); if (disco.IsError) throw new Exception(disco.Error);

var token = await client.RequestAuthorizationCodeTokenAsync(new AuthorizationCodeTokenRequest { Address = disco.TokenEndpoint, ClientId = "interactive", Client

相关内容

热门资讯

技术分享(德州Wepoke)外... 技术分享(德州Wepoke)外挂透明挂辅助APP(透视)透视辅助(2022已更新)(哔哩哔哩);1....
透视好友(微扑克外挂)外挂透明... 透视好友(微扑克外挂)外挂透明挂辅助器(辅助挂)发牌规律(果真有挂)-哔哩哔哩;亲们利用一分钟了解精...
避坑细节!德扑之星ai代打(W... 避坑细节!德扑之星ai代打(Wepoke)外挂透明挂辅助app(透视)必赢方法(详细教程)-哔哩哔哩...
五分钟了解(德扑之星app)外... 五分钟了解(德扑之星app)外挂透明挂辅助脚本(辅助挂)透视辅助(有挂方针)-哔哩哔哩;亲,其实确实...
分享个大家(Wepoke大厅)... 分享个大家(Wepoke大厅)外挂透明挂辅助APP(透视)软件透明挂(2021已更新)(哔哩哔哩),...
必备攻略!Wepoke透视(w... 必备攻略!Wepoke透视(wepOkE)外挂透明挂辅助APP(辅助挂)微扑克教程(有挂分析)-哔哩...
查到实测(Wepoke数据)外... 查到实测(Wepoke数据)外挂透明挂辅助软件(透视)透视辅助(有挂规律)-哔哩哔哩1、不需要AI权...
透明黑科技(Wepoke盈利)... 透明黑科技(Wepoke盈利)外挂透明挂辅助机制(透视)透视辅助(2022已更新)(哔哩哔哩)科技教...
今日科普!wepoke透明挂(... 相信很多朋友都在电脑上玩过wepoke透明挂吧,但是很多朋友都在抱怨用电脑玩起来不方便。为此小编给大...
透视科技(wpk教程)外挂透明... 透视科技(wpk教程)外挂透明挂辅助器(辅助挂)发牌机制(确实有挂)-哔哩哔哩;一、wpk教程有挂的...