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




27 January 2013

MVC- Authentication/Authorization









            //
            // GET: /Index/
            [Authorize]
            public ActionResult Index()
            {
                return View();
            }
        




            [Authorize]
            public class IndexController : Controller
            {
                //
                // GET: /Index/
                public ActionResult Index()
                {
                    return View();
                }

            }
        







            For Controller:

            [Authorize(Roles="Administrator, User")]
            public class IndexController : Controller
            {
                //
                // GET: /Index/

                public ActionResult Index()
                {
                    return View();
                }

            }

            For Action Method:

            //
            // GET: /Index/
            [Authorize(Roles = "Administrator, User")]
            public ActionResult Index()
            {
                return View();
            }

            [Authorize(Roles = "User")]
            public ActionResult User()
            {
                return View();
            }

        










            public class CustomAuth : AuthorizeAttribute
            {
                 public override void OnAuthorization(
                            AuthorizationContext filterContext)
                   {
                    base.OnAuthorization(filterContext);
                    if (filterContext.RequestContext.
                            HttpContext.User.Identity.IsAuthenticated)
                    {
                        //TODO: Add code for validate user
                    }
          }
      }


            For use this atherization code see below code block.


            [CustomAuth]
            public ActionResult Index1()
            {
                return View();
            }

        




            public class CustomAuth : AuthorizeAttribute
            {
                public string[] _roles;
                public CustomAuth()
                {
                    _roles = new string[] { "Administrator" };
                }

                public CustomAuth(string[] roles)
                {
                    _roles = roles;
                }

                public override void OnAuthorization(
                        AuthorizationContext filterContext)
                {
                    base.OnAuthorization(filterContext);
                    if (filterContext.RequestContext.
                            HttpContext.User.Identity.IsAuthenticated)
                    {
                        //TODO: Add code for validate user
                        //TODO: Check Roles
                    }
                }
            }



            [CustomAuth(new [] { "Administrator", "User" })]
            public ActionResult Index2()
            {
                return View();
            }

        


1 January 2013

At End of year 2012