Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OAuthOptions.Scope Cannot Be Assigned To

I'm trying to implement LinkedIn/OAuth authentication in my ASP.NET Core 2.0 app and I need to set the scope to { "r_basicprofile", "r_emailaddress" } so that I can user's email, profile image, etc.

When I try to set the scope in the following code, I'm getting the following error:

Property or indexer 'OAuthOptions.Scope' cannot be assigned to -- it's read-only.

Here's the code:

services.AddOAuth(CookieAuthenticationDefaults.AuthenticationScheme, options => {

   options.SignInScheme = "LinkedIn";
   options.ClientId = "1234567890";
   options.ClientSecret = "1234567890";
   options.CallbackPath = "/linkedin-callback";

   // Configure the LinkedIn endpoints                
   options.AuthorizationEndpoint = "https://www.linkedin.com/oauth/v2/authorization",
   options.TokenEndpoint = "https://www.linkedin.com/oauth/v2/accessToken",
   options.UserInformationEndpoint = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,picture-url,picture-urls::(original))",

   options.Scope = { "r_basicprofile", "r_emailaddress" };

   options.Events = new OAuthEvents
   {
        OnCreatingTicket = OnCreatingTicketLinkedInCallBack,
        OnTicketReceived = OnTicketReceivedCallback
   };
})

Any idea how I can set the scope?

P.S. I tried to adapt the code from my ASP.NET Core 1.1. This code was working fine in the ASP.NET Core 1.1 app.

like image 681
Sam Avatar asked Aug 19 '17 22:08

Sam


1 Answers

The Scope = { "r_basicprofile", "r_emailaddress" } syntax is only available when using object initialization. Here, the options object is not instantiated from your code but directly provided by ASP.NET Core so you can't use this syntax.

The good news is that Scope property is a collection (internally, a hash set) so you can simply do:

options.Scope.Add("r_basicprofile");
options.Scope.Add("r_emailaddress");

If you want to get rid of the default scopes, you can remove them using options.Scope.Remove("scope") or options.Scope.Clear().

like image 194
Kévin Chalet Avatar answered Oct 25 '22 15:10

Kévin Chalet