CheckerPhil
CheckerPhil

Reputation: 173

Authentication with Github in Asp Net Core 6

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.

Upvotes: 5

Views: 4602

Answers (2)

Ogglas
Ogglas

Reputation: 70008

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:

Third-party NuGet packages, such as the ones maintained by aspnet-contrib, can be used to complement the authentication providers implemented by the ASP.NET Core team

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:

services.AddAuthentication()
    .AddGitHub(options =>
    {
        options.ClientSecret = "<ClientSecret>";
        options.ClientId = "<ClientId>";
        options.CallbackPath = "/signin-oidc-github";
    })
    .AddIdentityServerJwt();

Github SSO will then look like this:

enter image description here

If you want the GitHub users email the code should look like this:

services.AddAuthentication()
    .AddGitHub(options =>
    {
        options.ClientSecret = "<ClientSecret>";
        options.ClientId = "<ClientId>";
        options.CallbackPath = "/signin-oidc-github";
        options.Scope.Add("user:email");
    })
    .AddIdentityServerJwt();

enter image description here

https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps

Upvotes: 3

Jason Pan
Jason Pan

Reputation: 21916

Please use AspNet.AspNet.Security.OAuth.GitHub and OctoKit library. You need run the install command in your project path. Like:

PM> cd .\GithubAuth
PM> Install-Package AspNet.Security.OAuth.GitHub -Version 6.0.5
PM> Install-Package Octokit -Version 0.50.0

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

Upvotes: 2

Related Questions