I want to add Authentication with GitHub to my Asp Net Core 6 project, but everything I find in the Internet is for older versions of Asp Net. How can I add it?
I added the Microsoft.AspNetCore.Authentication.OAuth
package to my project and created an Github OAuth application. I also added the client id and client secret with
dotnet user-secrets set Github:ClientId <clientid>
dotnet user-secrets set Github:ClientSecret <clientsecret>
I searched for tutorials online, but there are only tutorials using a Asp Net Version that is less than 6.
2
Answers
Please use
AspNet.AspNet.Security.OAuth.GitHub
andOctoKit
library. You need run the install command in your project path. Like:I have test in my dotnet core 6 app, and it works fine.
For more details, please check below blogs:
In this blogs, it mentions Linux platform, you can ignore it.
Add GitHub OpenID Auth For ASP.NET Core Apps
This is the instructions from External OAuth authentication providers for
ASP.NET Core 7.0
https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/other-logins?view=aspnetcore-7.0
Register a new OAuth application here:
https://github.com/settings/applications/new
If you want your organisation to own it then use this guide instead:
https://docs.github.com/en/developers/apps/building-oauth-apps/creating-an-oauth-app
Docs for Authorizing OAuth Apps
https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps
Microsofts documentation says:
This is their NuGet:
https://www.nuget.org/packages/AspNet.Security.OAuth.GitHub
If you receive the error
Unable to unprotect the message.State.
check that your CallbackPath is unique if you have several OIDC connections.You can then add this code:
Github SSO will then look like this:
If you want the GitHub users email the code should look like this:
https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps