Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login with LinkedIn account. #414

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions EmpleoDotNet.AppServices/UserProfileSocialService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,26 @@ public UserProfile GetFromSocialProvider(string socialProvider, ClaimsIdentity i
return GetFromGoogle(identity);
case "Microsoft":
return GetFromMicrosoft(identity);
case "LinkedIn":
return GetFromLinkedIn(identity);
default:
return new UserProfile();
}
}

private UserProfile GetFromLinkedIn(ClaimsIdentity identity)
{
return FromCommonClaimsIdentity(identity);
}

private UserProfile GetFromMicrosoft(ClaimsIdentity identity)
{
var email = identity.FindFirst(x => x.Type.Contains("emailaddress")).Value;
var name = identity.Name;
return new UserProfile
{
Email = email,
Name = name
};
return FromCommonClaimsIdentity(identity);
}

private UserProfile GetFromGoogle(ClaimsIdentity identity)
{
var email = identity.FindFirst(x => x.Type.Contains("emailaddress")).Value;
var name = identity.Name;
return new UserProfile
{
Email = email,
Name = name
};
return FromCommonClaimsIdentity(identity);
}

private UserProfile GetFromFacebook(ClaimsIdentity identity)
Expand All @@ -59,5 +54,18 @@ private UserProfile GetFromFacebook(ClaimsIdentity identity)
Name = fbInfo.name
};
}

private UserProfile FromCommonClaimsIdentity(ClaimsIdentity identity)
{
var email = identity.FindFirst(x => x.Type.Contains("emailaddress"))?.Value ?? string.Empty;

var name = identity.Name;

return new UserProfile
{
Email = email,
Name = name
};
}
}
}
4 changes: 3 additions & 1 deletion EmpleoDotNet.Repository/UserProfileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public UserProfileRepository(EmpleadoContext context) : base(context)

public UserProfile GetByUserId(string userId)
{
return DbSet.Where(x => x.UserId == userId).Include(x=>x.JobOpportunities).FirstOrDefault();
return DbSet.Where(x => x.UserId == userId)
.Include(x => x.JobOpportunities.Select(j => j.JobOpportunityLikes))
.FirstOrDefault();
}
}
}
20 changes: 14 additions & 6 deletions EmpleoDotNet/App_Start/Startup.Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Owin.Security.Google;
using Microsoft.Owin.Security.MicrosoftAccount;
using Owin;
using Owin.Security.Providers.LinkedIn;
using Tweetinvi;

namespace EmpleoDotNet
Expand All @@ -35,33 +36,40 @@ public void ConfigureAuth(IAppBuilder app)
msAuthOptions.ClientSecret = ConfigurationManager.AppSettings["msClientSecret"];
app.UseMicrosoftAccountAuthentication(msAuthOptions);

// app.UseTwitterAuthentication(
// consumerKey: ConfigurationManager.AppSettings["consumerKey"],
// consumerSecret: ConfigurationManager.AppSettings["consumerSecret"]
// );
// app.UseTwitterAuthentication(
// consumerKey: ConfigurationManager.AppSettings["consumerKey"],
// consumerSecret: ConfigurationManager.AppSettings["consumerSecret"]
// );

var fbAuthOptions = new FacebookAuthenticationOptions
{
AppId = ConfigurationManager.AppSettings["fbAppId"],
AppSecret = ConfigurationManager.AppSettings["fbAppSecret"]
};

fbAuthOptions.Scope.Add("email");
fbAuthOptions.Scope.Add("public_profile");
fbAuthOptions.Scope.Add("user_friends");
fbAuthOptions.Provider = new FacebookAuthenticationProvider
{
OnAuthenticated = context =>
{
context.Identity.AddClaim(new Claim("FacebookAccessToken",context.AccessToken));
context.Identity.AddClaim(new Claim("FacebookAccessToken", context.AccessToken));
return Task.FromResult(true);
}
};
app.UseFacebookAuthentication(fbAuthOptions);

app.UseGoogleAuthentication(
clientId: ConfigurationManager.AppSettings["googleClientId"],
clientSecret: ConfigurationManager.AppSettings["googleClientSecret"]
);

app.UseLinkedInAuthentication(new LinkedInAuthenticationOptions
{
ClientId = ConfigurationManager.AppSettings["linkedinClientId"],
ClientSecret = ConfigurationManager.AppSettings["linkedinClientSecret"]
});
}
}
}
2 changes: 2 additions & 0 deletions EmpleoDotNet/Base_Auth.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
<add key="msClientSecret" value="" />
<add key="googleClientId" value="" />
<add key="googleClientSecret" value="" />
<add key="linkedinClientId" value="" />
<add key="linkedinClientSecret" value="" />
</appSettings>
2 changes: 1 addition & 1 deletion EmpleoDotNet/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
user = _authenticationService.CreateUserWithSocialProvider(login, result.Identity);
await SignInAsync(user, isPersistent: false).ConfigureAwait(false);

return RedirectToAction("Profile", new { returnUrl });
return RedirectToAction("Profile", new {returnUrl});
}
catch (Exception ex)
{
Expand Down
4 changes: 4 additions & 0 deletions EmpleoDotNet/EmpleoDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Owin.Security.Providers, Version=1.25.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Owin.Security.Providers.1.27\lib\net45\Owin.Security.Providers.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="reCAPTCHA.MVC, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\reCAPTCH.MVC.1.0.1\lib\net45\reCAPTCHA.MVC.dll</HintPath>
<Private>True</Private>
Expand Down
10 changes: 8 additions & 2 deletions EmpleoDotNet/Views/Account/_ExternalLoginsListPartial.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="container">
<div class="row">
<div>
<h2>Inicia sesión</h2>
<h2>Inicia sesión</h2>
</div>
<div class="row">
<div class="col-sm-4">
Expand All @@ -23,7 +23,7 @@
{
string action = Model.Action;
string returnUrl = Model.ReturnUrl;
using (Html.BeginForm(action, "Account", new {ReturnUrl = returnUrl}))
using (Html.BeginForm(action, "Account", new { ReturnUrl = returnUrl }))
{
@Html.AntiForgeryToken()
<div id="socialLoginList">
Expand Down Expand Up @@ -55,6 +55,12 @@
<span class="fa fa-twitter"></span> Accede con Twitter
</button>
}
else if (p.AuthenticationType == "LinkedIn")
{
<button name="provider" value="@p.AuthenticationType" type="submit" class="btn btn-block btn-social btn-linkedin">
<span class="fa fa-linkedin"></span> Accede con LinkedIn
</button>
}
else
{
<button type="submit" class="btn btn-block btn-social btn-primary" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">
Expand Down
1 change: 1 addition & 0 deletions EmpleoDotNet/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<package id="Ninject.Web.Common" version="3.2.0.0" targetFramework="net45" />
<package id="Ninject.Web.Common.WebHost" version="3.2.0.0" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />
<package id="Owin.Security.Providers" version="1.27" targetFramework="net45" />
<package id="reCAPTCH.MVC" version="1.0.1" targetFramework="net45" />
<package id="Respond" version="1.4.2" targetFramework="net45" />
<package id="routedebugger" version="2.1.4.0" targetFramework="net45" />
Expand Down