I need to build an Authentication between my Angular frontend and ASP.NET Web Api backend.
The requirements are:
I can't find any up to date documentation about how I should make this... I looked to https://auth0.com/ but they are using a package that isn't supported any more by my web api version.
My research also learned me that I probably should use Jwt, but i'm having a difficult time to build this in my web api.
Can someone please give me proper documentation? I know there is a build-in authentication but I don't know how good this one is and how you should use them with JWT. Everything I find on the web is out-of-date... .
Your help is much appreciated.
This is the flow you need to do in order to authenticate a user with linkedin:
Angular Part:
1.redirect the user with the requested query params, for example:
var req = {
response_type: "code",
client_id: "Your Client ID",
redirect_uri: location.origin + location.pathname,
state: "DCESFWf45A53sdfKef434"
};
window.location.href = linkedinAuthorizationUrl + ObjecttoParams(req);
2.After the user is authenticated linkedin will redirect the user back to your redirect_url with additional query param named code, your angular controller should collect it and send it to your webapi method for the next step.
WebApi part:
3.Once you posted the linkedin code to your webapi you need to exchange token with linkedin api(HTTP POST), for example:
using (WebClient wc = new WebClient()){
string RedirectUrl = providerLogin.RedirectUrl;
//Exchange tokens with linkedin
byte[] resultAsBytes =
wc.UploadValues("https://www.linkedin.com/oauth/v2/accessToken", new
NameValueCollection()
{
{ "grant_type", "authorization_code" },
{ "code", "THE CODE YOU RECEIVED FROM THE CLIENT" },
{"redirect_uri",RedirectUrl },
{"client_id", "YOUR LINKEDIN KEY"},
{"client_secret", "YOUR LINKEDIN PASSWORD"}
});
string resultAsString =
System.Text.Encoding.UTF8.GetString(resultAsBytes);
var resultAsJson = JObject.Parse(resultAsString);
string accessToken =
resultAsJson["access_token"].ToString();
}
4.Last part - when you have the access token you need to perform GET request to linkedin's API in order to get the user's details:
using (WebClient wc = new WebClient()){
wc.Headers.Add("Authorization", "Bearer " + accessToken);
var json = wc.DownloadString("https://api.linkedin.com/v1/people/~:
(email-address,id,first-name,last-name,picture-url,public-profile-url)?
format=json");
resultAsJson = JObject.Parse(json);
}
And that's it! I hope everything is clear and you understand my code. Good luck!