Asp.net 如何在没有OAuth的情况下获得LinkedIn用户技能、教育和职位
我正在开发一个ASP.NET核心应用程序,其中用户必须使用单个数据库帐户。在所有教程中都明确指出,在创建项目时必须选择“无身份验证”。我不明白为什么 我想让用户能够登录LinkedIn,然后获得他们的邮件、姓名和其他信息。我可以通过以下方式实现:Asp.net 如何在没有OAuth的情况下获得LinkedIn用户技能、教育和职位,asp.net,oauth,Asp.net,Oauth,我正在开发一个ASP.NET核心应用程序,其中用户必须使用单个数据库帐户。在所有教程中都明确指出,在创建项目时必须选择“无身份验证”。我不明白为什么 我想让用户能够登录LinkedIn,然后获得他们的邮件、姓名和其他信息。我可以通过以下方式实现: app.UseLinkedInAuthentication(AuthenticationSettings.LinkedInOptions( Configuration["LinkedIn:ClientId"],
app.UseLinkedInAuthentication(AuthenticationSettings.LinkedInOptions(
Configuration["LinkedIn:ClientId"],
Configuration["LinkedIn:ClientSecret"]));
在Startup.cs和中(加上文档中的所有其他步骤)
var loginProviders=SignInManager.GetExternalAuthenticationSchemes().ToList();
如果(loginProviders.Count==0)
{
没有配置外部身份验证服务。请参阅
有关设置此ASP.NET应用程序以支持通过外部服务登录的详细信息,请参阅。
}
其他的
{
@foreach(loginProviders中的var提供程序)
{
@provider.AuthenticationScheme
}
}
在_Layout.cshtml中:
我按下按钮,要求用户授予应用程序权限,我获取个人数据,但我如何获取其他信息(技能、教育、职位等),如:
但是如果没有OAuth,或者在创建项目时如何在选择授权的情况下实现OAuth?我使用的是单个数据库帐户,这就是我如何使其工作的: 在Startup.cs中
app.UseLinkedInAuthentication(AuthenticationSettings.LinkedInOptions(
Configuration["LinkedIn:ClientId"],
Configuration["LinkedIn:ClientSecret"]
创建一个类AuthentificationSettings,如下所示:
public class AuthenticationSettings
{
public static LinkedInAuthenticationOptions LinkedInOptions(string clientId, string clientSecret)
{
if (string.IsNullOrEmpty(clientId))
{
throw new ArgumentNullException($"{nameof(clientId)} is null or empty");
}
if (string.IsNullOrEmpty(clientSecret))
{
throw new ArgumentNullException($"{nameof(clientSecret)} is null or empty");
}
LinkedInAuthenticationOptions options = new LinkedInAuthenticationOptions
{
ClientId = clientId,
ClientSecret = clientSecret,
SaveTokens = true,
Events = new OAuthEvents
{
OnCreatingTicket = async ctx =>
{
ctx.Identity.AddClaim(new Claim("access_token", ctx.AccessToken));
ctx.Identity.AddClaim(new Claim("refresh_token", ctx.AccessToken));
ctx.Identity.AddClaim(new Claim("ExpiresIn", ctx.ExpiresIn.Value.Seconds.ToString()));
// request is the GET request for linkedin
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.linkedin.com/v1/people/~:(first-name,last-name,headline,picture-url,industry,summary,positions:(title,summary,start-date,end-date,company:(name,industry))) ");
request.Headers.Authorization =new AuthenticationHeaderValue("Bearer", ctx.AccessToken);
request.Headers.Add("x-li-format", "json");
var response = await ctx.Backchannel.SendAsync(request, ctx.HttpContext.RequestAborted);
response.EnsureSuccessStatusCode();
// In user is stored the profile details
var user = JObject.Parse(await response.Content.ReadAsStringAsync());
// Save the linkedin details in variabels
string json = user.ToString();
LinkedinCandidate linkedinProfile = JsonConvert.DeserializeObject<LinkedinCandidate>(json);
var firstName = linkedinProfile.firstName;
var lastName = linkedinProfile.lastName;
var headline = linkedinProfile.headline;
var pictureUrl = linkedinProfile.pictureUrl;
var summaryProfile = linkedinProfile.summary;
var summaryCOmpany = linkedinProfile.positions.values[0].summary;
var titleJob = linkedinProfile.positions.values[0].title;
var companyName = linkedinProfile.positions.values[0].company.name;
var industry = linkedinProfile.positions.values[0].company.industry;
var monthStart = linkedinProfile.positions.values[0].startDate.month;
var yearStart = linkedinProfile.positions.values[0].startDate.year;
await Task.FromResult(0);
},
OnRemoteFailure = ctx =>
{
// Handle remote errors
return Task.FromResult(0);
}
}
};
return options;
}
}
public class AuthenticationSettings
{
public static LinkedInAuthenticationOptions LinkedInOptions(string clientId, string clientSecret)
{
if (string.IsNullOrEmpty(clientId))
{
throw new ArgumentNullException($"{nameof(clientId)} is null or empty");
}
if (string.IsNullOrEmpty(clientSecret))
{
throw new ArgumentNullException($"{nameof(clientSecret)} is null or empty");
}
LinkedInAuthenticationOptions options = new LinkedInAuthenticationOptions
{
ClientId = clientId,
ClientSecret = clientSecret,
SaveTokens = true,
Events = new OAuthEvents
{
OnCreatingTicket = async ctx =>
{
ctx.Identity.AddClaim(new Claim("access_token", ctx.AccessToken));
ctx.Identity.AddClaim(new Claim("refresh_token", ctx.AccessToken));
ctx.Identity.AddClaim(new Claim("ExpiresIn", ctx.ExpiresIn.Value.Seconds.ToString()));
// request is the GET request for linkedin
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.linkedin.com/v1/people/~:(first-name,last-name,headline,picture-url,industry,summary,positions:(title,summary,start-date,end-date,company:(name,industry))) ");
request.Headers.Authorization =new AuthenticationHeaderValue("Bearer", ctx.AccessToken);
request.Headers.Add("x-li-format", "json");
var response = await ctx.Backchannel.SendAsync(request, ctx.HttpContext.RequestAborted);
response.EnsureSuccessStatusCode();
// In user is stored the profile details
var user = JObject.Parse(await response.Content.ReadAsStringAsync());
// Save the linkedin details in variabels
string json = user.ToString();
LinkedinCandidate linkedinProfile = JsonConvert.DeserializeObject<LinkedinCandidate>(json);
var firstName = linkedinProfile.firstName;
var lastName = linkedinProfile.lastName;
var headline = linkedinProfile.headline;
var pictureUrl = linkedinProfile.pictureUrl;
var summaryProfile = linkedinProfile.summary;
var summaryCOmpany = linkedinProfile.positions.values[0].summary;
var titleJob = linkedinProfile.positions.values[0].title;
var companyName = linkedinProfile.positions.values[0].company.name;
var industry = linkedinProfile.positions.values[0].company.industry;
var monthStart = linkedinProfile.positions.values[0].startDate.month;
var yearStart = linkedinProfile.positions.values[0].startDate.year;
await Task.FromResult(0);
},
OnRemoteFailure = ctx =>
{
// Handle remote errors
return Task.FromResult(0);
}
}
};
return options;
}
}
{ "firstName": "",
"headline": "",
"lastName": "",
"pictureUrl": "",
"positions": {
"_total": 1,
"values": [ {
"company": {
"industry": "",
"name": "" },
"startDate": {
"month": ,
"year":
},
"summary": "",
"title": ""
}
]
},
"summary": ""}