Example implementation of OAuth2 for Discord using Blazor
- Edit
appsettings.json.template
to include your app id and secret found at https://discordapp.com/developers/applications/ then rename the file toappsettings.json
- (optional) Edit
launchsettings.json
and setsslPort
to your desired port, this will be used in step 3 - Go to your application at https://discordapp.com/developers/applications/[APP_ID]/oauth and add a redirect to
https://localhost:PORT/signin-discord
, replacingPORT
with the one you chose in step 2 (or 44393 if you skipped step 2) - Add any necessary scopes in
startup.cs
NOTE: Currently email, identify and guilds are supported. Others may not work without editingDiscordHandler.cs
and adding requesting/parsing data yourself.
- use AuthorizeView for content that can be seen but changes based on whether a user is authorized or not
Example:
MainLayour.razor
<AuthorizeView>
<Authorized>
<a href="Account/Manage">Hello, @context.User.Identity.Name!</a>
<a href="Account/LogOut">LogOut</a>
</Authorized>
<NotAuthorized>
<a href="Account/Login">Log in</a>
</NotAuthorized>
</AuthorizeView>
- use
@attribute [Authorize]
as a page header to ensure a user is authorized before accessing the page. (demonstrated inAccountManage.razor
) NOTE:App.razor
has been configured viaAuthorizeRouteView
to automatically redirect a user to the discord auth page if they access an authorized page and have not authorized themselves