LinkedIn Authentication using OAuth2.0 in ASP.NET Core 2.0

Refresh

April 2019

Views

347 time

1

I am trying to use the LinkedIn authentication in my asp.net core 2.0 app. I am following this article as an example, but its using Core 1.0:

Authenticating a user with LinkedIn in ASP.NET Core

Some of the code updates to move to Core 2.0 I have made are in the registration of the middleware:

    public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie(options =>
            {
                options.LoginPath = new PathString("/login");
                options.LogoutPath = new PathString("/logout");

            })
           .AddOAuth("LinkedIn", options =>
            {
                options.ClientId = Configuration["linkedin:clientId"];
                options.ClientSecret = Configuration["linkedIn:clientSecret"];
                options.CallbackPath = new PathString("/signin-linkedin");
                options.AuthorizationEndpoint = "https://www.linkedin.com/oauth/v2/authorization";
                options.UserInformationEndpoint =
                    "https://api.linkedin.com/v1/people/~:(id,formatted-name,email-address,picture-url)";
                options.Scope.Add("r_basicprofile");
                options.Scope.Add("r_emailaddress");
                options.Events = new OAuthEvents()
                {
                    // The OnCreatingTicket event is called after the user has been authenticated and the OAuth middleware has 
                    // created an auth ticket. We need to manually call the UserInformationEndpoint to retrieve the user's information,
                    // parse the resulting JSON to extract the relevant information, and add the correct claims.
                    OnCreatingTicket = async context =>

I am stuck with what changes are required to map the login and logout routes as explained in the article.

    // Listen for requests on the /login path, and issue a challenge to log in with the LinkedIn middleware
app.Map("/login", builder =>
{
    builder.Run(async context =>
    {
        // Return a challenge to invoke the LinkedIn authentication scheme
        await context.Authentication.ChallengeAsync("LinkedIn", properties: new AuthenticationProperties() { RedirectUri = "/" });
    });
});

// Listen for requests on the /logout path, and sign the user out
app.Map("/logout", builder =>
{
    builder.Run(async context =>
    {
        // Sign the user out of the authentication middleware (i.e. it will clear the Auth cookie)
        await context.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

        // Redirect the user to the home page after signing out
        context.Response.Redirect("/");
    });
});

What changes would I need to make to register the login and logout routes in Core 2.0?

0 answers