无法将 OAuthOptions.Scope 分配给

标签 oauth asp.net-core asp.net-authentication

我正在尝试在 ASP.NET Core 2.0 应用程序中实现 LinkedIn/OAuth 身份验证,并且需要将范围设置为 { "r_basicprofile", "r_emailaddress"} 以便我可以将用户的电子邮件、个人资料图片等

当我尝试在以下代码中设置范围时,出现以下错误:

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

代码如下:

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
   };
})

知道如何设置范围吗?

附注我尝试改编 ASP.NET Core 1.1 中的代码。此代码在 ASP.NET Core 1.1 应用程序中运行良好。

最佳答案

Scope = { "r_basicprofile", "r_emailaddress"} 语法仅在使用对象初始化时可用。在这里,options 对象不是从您的代码实例化的,而是由 ASP.NET Core 直接提供的,因此您不能使用此语法。

好消息是 Scope 属性是一个集合(内部是一个哈希集),因此您可以简单地执行以下操作:

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

如果您想删除默认范围,可以使用 options.Scope.Remove("scope")options.Scope.Clear() 删除它们>.

关于无法将 OAuthOptions.Scope 分配给,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45776947/

相关文章:

php - 在 Symfony 中使用 Mautic api 库时尝试调用名为 "validateAccessToken"的未定义方法

c# - 如何在 ASP.NET Core 中增强 ModelBinder(更新模型类的属性值)

c# - ASP.NET Core错误:System.TypeLoadException:无法加载类型 'Microsoft.AspNetCore.WebUtilities.FileBufferingWriteStream'

asp.net - Asp Core 2.0 和 Identity Server v3,受众验证失败

asp.net - 我需要对用户进行身份验证和授权的最低 ASP.NET 提供程序实现是什么?

ASP.NET Web Api (REST) : Authentication using the users credentials or a token? 保留 "Register new user"资源密码吗?

python - 使用 oauth 将 GAE python 应用程序连接到 google 的 drive/docs/spreadsheet

authentication - 使用 OAuth 和持久刷新 token 的 ADFS 3.0

java - 使用 oAuth - fatsecret API 向 REST API 发送请求

c# - 从域模型:MVVM创建 View 模型