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
            }

        




           
            1: Logon
           
            @model ICollection
            @{
                ViewBag.Title = "Logon";
            }
            

Logon

@using (Html.BeginForm()) {
@foreach (var item in Model) { }
} 2: LoginConfirm @{ ViewBag.Title = "LoginConfirm"; }

LoginConfirm

@ViewBag.UserName




1 comment:

  1. How is the different than the template that comes with VS2012? Just curious to see if I am missing anything important.

    ReplyDelete