aspnet-contrib / AspNet.Security.OAuth.Providers

OAuth 2.0 social authentication providers for ASP.NET Core
Apache License 2.0
2.34k stars 533 forks source link

How do I update Claims (Discord provider)? #840

Closed XShine555 closed 4 months ago

XShine555 commented 4 months ago

I am quite lost and I have been trying to update the user and avatar every time the user enters my site for several hours, because if the user after logging in with discord changes the name, avatar or other information is not updated once the user re-enters the page. How could I solve this? So far I have done the basic authentication and I am stuck with this problem.

builder.Services.AddAuthentication(x => x.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(x =>
    {
        x.LoginPath = "/signin";
        x.LogoutPath = "/signout";
    })
    .AddDiscord(x =>
    {
        x.ClientId = "Client";
        x.ClientSecret = "Secret";
    });
XShine555 commented 4 months ago

Nvm I figured out how to do it.

public async Task UpdateClaimNameAsync(HttpContext httpContext, string value)
{
    var identity = httpContext.User.Identity;
    (identity as ClaimsIdentity).RemoveClaim(httpContext.User.FindFirst(ClaimTypes.Name));
    (identity as ClaimsIdentity).AddClaim(new Claim(ClaimTypes.Name, value));
}