Software consultant, Developer, Codder : SharePoint, Office 365, ASP.NET, MVC, SharePoint, Javascript, JQuery, Ember JS, Angular JS, Entity Framework, Linq, HTML5, DotnetNuke, codding philosophy, blogger 7+ experience in codding and developing web applications, Code 4 Help..
29 January 2013
MVC – Authentication using OAuth
public static class OAuthReg
{
public static void RegisterOAuth()
{
OAuthWebSecurity.RegisterLinkedInClient(
consumerKey: "111111",
consumerSecret: "11111111");
OAuthWebSecurity.RegisterTwitterClient(
consumerKey: "111111",
consumerSecret: "11111111");
OAuthWebSecurity.RegisterFacebookClient(
appId: "1111111",
appSecret: "11111111");
OAuthWebSecurity.RegisterGoogleClient();
}
}
For register we need to add code in Global.asax file in Application Start Method
Models.OAuthReg.RegisterOAuth();
public class HomeController : Controller
{
[HttpPost]
public ActionResult Logon(string provider, string returnUrl)
{
return new ExternerLoginResult(provider,
Url.Action("LogonCallBack", new { returnUrl }));
}
[HttpPost]
public ActionResult LogonCallBack(string returnUrl)
{
AuthenticationResult result =
OAuthWebSecurity.VerifyAuthentication(
Url.Action("LogonCallBack", new { ReturnUrl = returnUrl }));
if (!result.IsSuccessful)
return new EmptyResult();
ViewBag.UserName = result.UserName;
return View("LoginConfirm");
}
#region
internal class ExternerLoginResult : ActionResult
{
public ExternerLoginResult(string _provider,
string _returnUrl)
{
this.Provider = _provider;
this.ReturnUrl = _returnUrl;
}
public string Provider { get; set; }
public string ReturnUrl { get; set; }
public override void ExecuteResult(ControllerContext context)
{
OAuthWebSecurity.RequestAuthentication(this.Provider,
this.ReturnUrl);
}
}
#endregion
}
How is the different than the template that comes with VS2012? Just curious to see if I am missing anything important.
ReplyDelete