I found a lot of posts and articles with very detailed information about how to configure MVC 4 application to be integrated with any social network provider and how to authenticate users, but what's next? How to get any information about authenticated user, for example? The simplest task that coming to my head how to get some information about authenticated user - first name, last name, avatar's url, friends list etc.?
Update:
Let's create a sample Internet MVC 4 application and name it GoogleAuthSample. Go to: "App_Start\AuthConfig.cs" and uncomment the following line of code: We have integrated Google OAuth with the application. Run the application, there will a button added automatically in the Login page as shown below:
If you need an introduction to ASP.NET MVC 4, see Intro to ASP.NET MVC 4. In Visual Studio, create a new ASP.NET MVC 4 Web Application, and name it "OAuthMVC". You can target either .NET Framework 4.5 or 4. In the New ASP.NET MVC 4 Project window, select Internet Application and leave Razor as the view engine.
OAuth authorization is an open standard for authorization using third party applications. OAuth or Open standard for Auhtorization has become a standard which is used nowdays in most of the applications. Here we will discuss what is OAuth and how we can implement it using ASP.NET MVC.
Visual studio 2012 provides OAuth support out of the box for different types of ASP.NET applications such as Web forms and MVC. Following are the steps to create an MVC application that uses OAuth to authenticate the user using his Facebook account. In the new project dialog select the ASP.NET MVC 4 application in the templates list.
OAuth is for authenticating only, i.e. getting an access token. Once you get this access token you could use it to retrieve this information from the service provider. Consult the documentation of the provider to see how this could be done.
There are some claims you might retrieve though such as FirstName and LastName because they are standard and most providers support them. For example inside the ExternalLoginCallback
callback you could attempt to retrieve this information from the result.ExtraData
dictionary:
[AllowAnonymous]
public ActionResult ExternalLoginCallback(string returnUrl)
{
AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
if (!result.IsSuccessful)
{
return RedirectToAction("ExternalLoginFailure");
}
if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
{
return RedirectToLocal(returnUrl);
}
if (User.Identity.IsAuthenticated)
{
// Here you could use result.ExtraData dictionary
string name = result.ExtraData["name"];
// If the current user is logged in add the new account
OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);
return RedirectToLocal(returnUrl);
}
else
{
// User is new, ask for their desired membership name
string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName;
ViewBag.ReturnUrl = returnUrl;
return View("ExternalLoginConfirmation", new RegisterExternalLoginModel { UserName = result.UserName, ExternalLoginData = loginData });
}
}
But the different providers might use different keys. So depending on the provider that was used you will have to use the proper key to read the desired information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With