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状态丢失或无效的问题。

相关内容

热门资讯

第七分钟辅助!wepoker辅... 【福星临门,好运相随】;第七分钟辅助!wepoker辅助器是真的吗,闲逸斗地主辅助(透视app开挂辅...
八分钟辅助!智星菠萝可以辅助吗... 【福星临门,好运相随】;八分钟辅助!智星菠萝可以辅助吗,新道游开挂辅助透视(重大通报开挂辅助神器);...
辅助开挂!四川游戏家园辅助软件... 辅助开挂!四川游戏家园辅助软件,hhpkoer辅助器(透视)开挂辅助软件(有人有挂);1、不需要AI...
辅助开挂!博雅红河西元红河破解... 辅助开挂!博雅红河西元红河破解版,德州透视是真的吗(透视)开挂辅助平台(新版有挂);博雅红河西元红河...
开挂辅助!好友赣南新版本可以作... 开挂辅助!好友赣南新版本可以作假吗,hhpoker免费透视脚本(透视)开挂辅助神器(讲解有挂);小薇...
开挂辅助!微乐小程序辅助,德州... 开挂辅助!微乐小程序辅助,德州局hhpoker(透视)开挂辅助工具(有挂辅助);1、让任何用户在无需...
开挂辅助!雀友会潮汕麻将开挂辅... 开挂辅助!雀友会潮汕麻将开挂辅助,wepoker永久免费脚本(透视)开挂辅助软件(的确有挂);1、很...
辅助开挂!博弈麻友圈辅助,we... 辅助开挂!博弈麻友圈辅助,we poker游戏下(透视)开挂辅助插件(有挂教程);亲,有的,ai轻松...
开挂辅助!九九山城辅助,红龙p... 开挂辅助!九九山城辅助,红龙poker辅助(透视)开挂辅助插件(有挂规律);是一款可以让一直输的玩家...
辅助开挂!拼十app辅助,we... 辅助开挂!拼十app辅助,wepoker透视有吗(透视)开挂辅助工具(有挂分析);拼十app辅助软件...