ASP.NET Core MVC : Discord Oauth2请求 - Oauth状态丢失或无效
创始人
2024-09-15 06:30:46
0

以下是解决ASP.NET Core MVC中Discord Oauth2请求中Oauth状态丢失或无效的示例代码:

  1. 确保在Startup.cs文件中正确配置Discord Oauth2:
public void ConfigureServices(IServiceCollection services)
{
    // 添加Discord认证服务
    services.AddAuthentication(options =>
    {
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = "Discord";
    })
    .AddCookie()
    .AddOAuth("Discord", options =>
    {
        options.ClientId = "YOUR_DISCORD_CLIENT_ID";
        options.ClientSecret = "YOUR_DISCORD_CLIENT_SECRET";
        options.CallbackPath = new PathString("/signin-discord");
        options.AuthorizationEndpoint = "https://discord.com/api/oauth2/authorize";
        options.TokenEndpoint = "https://discord.com/api/oauth2/token";
        options.UserInformationEndpoint = "https://discord.com/api/users/@me";
        options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
        options.ClaimActions.MapJsonKey(ClaimTypes.Name, "username");
        options.ClaimActions.MapJsonKey("urn:discord:avatar", "avatar");
        options.SaveTokens = true;
    });

    // 添加MVC服务
    services.AddMvc();
}
  1. 在Controller中使用Discord Oauth2进行身份验证:
public class DiscordController : Controller
{
    [HttpGet]
    public IActionResult Login()
    {
        // 重定向到Discord Oauth2认证
        return Challenge(new AuthenticationProperties { RedirectUri = "/" }, "Discord");
    }

    [HttpGet]
    public async Task Callback()
    {
        // 获取Discord用户的身份信息
        var authenticateResult = await HttpContext.AuthenticateAsync("Discord");
        var userId = authenticateResult.Principal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
        var username = authenticateResult.Principal.FindFirst(ClaimTypes.Name)?.Value;
        var avatarUrl = authenticateResult.Principal.FindFirst("urn:discord:avatar")?.Value;

        // 处理身份信息,例如保存到数据库或创建用户会话
        // ...

        return RedirectToAction("Index", "Home");
    }
}
  1. 添加Discord登录按钮到视图页面:
@using Microsoft.AspNetCore.Authentication

@if (User.Identity.IsAuthenticated)
{
    

Welcome, @User.Identity.Name!

} else { Discord Login }

通过以上代码示例,您可以在ASP.NET Core MVC应用程序中使用Discord Oauth2进行身份验证,并解决Oauth状态丢失或无效的问题。

相关内容

热门资讯

wepoke辅助机器人!智星德... wepoke辅助机器人!智星德州菠萝开挂,(德州aa扑克)起初是有挂(详细辅助透视教程)关于智星德州...
透视系统(智星德州菠萝外挂)德... 透视系统(智星德州菠萝外挂)德扑之星怎么设置(详细辅助解密教程)原本是有挂1、下载好辅助软件之后点击...
透视线上(wepokeai代打... 透视线上(wepokeai代打)微扑克系统的发牌速度有多快(详细辅助AA德州教程)从来是真的有挂是一...
aapoker挂!wpk微扑克... aapoker挂!wpk微扑克真的有辅助插件吗,(AAPoker)固有存在有挂(详细辅助AI教程);...
透视软件(aapoker发牌机... 透视软件(aapoker发牌机制)wpk德州有挂吗(详细辅助透视教程)一向真的是有挂您好,,确实是有...
微扑克有辅助挂!菠萝德州app... 微扑克有辅助挂!菠萝德州app有挂吗,(wepOkE)往昔真的是有挂(详细辅助曝光教程);大神普及一...
透视神器(wepoke模拟器)... 透视神器(wepoke模拟器)aapoker发牌机制(详细辅助2025新版教程)本然真的有挂1、用户...
wpk透明挂!智星德州辅助,(... wpk透明挂!智星德州辅助,(手机德州)一向是真的有挂(详细透视wpk教程);玩家必备必赢加哟《13...
透视能赢(德州ai人工智能)德... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏中打牌都...
wpk真的有外挂!德州ai智能... wpk真的有外挂!德州ai智能营销系统,(线上wpk德州)最初有挂(详细辅助2025版教程);wpk...