Ejemplo n.º 1
0
        // GET: FaceBookLogin
        public ActionResult Index()
        {
            KaribouAlpha.Models.FaceBookClient client = db.FaceBookClients.SingleOrDefault(_fb => _fb.Active);
            var url = string.Format("https://www.facebook.com/v2.11/dialog/oauth?client_id={0}&redirect_uri={1}&state={2}&response_type=code&scope=email", client.AppId, client.CallBackUrl, System.Guid.NewGuid().ToString());

            return(Redirect(url));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> CallBack(string code)
        {
            string path     = "";
            bool   writeLog = false;

            if (System.Configuration.ConfigurationManager.AppSettings["DebugLogFile"] != null)
            {
                if (string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["DebugLogFile"].ToString()) == false)
                {
                    path     = System.Configuration.ConfigurationManager.AppSettings["DebugLogFile"].ToString();
                    writeLog = true;
                }
            }

            if (writeLog)
            {
                System.IO.File.AppendAllText(path, Environment.NewLine + System.DateTime.Now.ToString() + " called..fb callback");
                System.IO.File.AppendAllText(path, Environment.NewLine + System.DateTime.Now.ToString() + " code is : " + Environment.NewLine + code);
            }

            KaribouAlpha.Models.FaceBookClient client = db.FaceBookClients.SingleOrDefault(_fb => _fb.Active);
            if (client == null)
            {
                ViewBag.EmailError = true;
                return(View("CallBack"));
            }

            //verifyTokenEndPoint = string.Format("https://graph.facebook.com/debug_token?input_token={0}&access_token={1}", code, appToken);
            //HttpResponseMessage response;
            //Uri uri = new Uri(verifyTokenEndPoint);

            //using (HttpClient httpClient = new HttpClient())
            //{
            //    response = await httpClient.GetAsync(uri);
            //}

            //if (response.IsSuccessStatusCode)
            //{
            //    System.IO.File.AppendAllText(path, Environment.NewLine + System.DateTime.Now.ToString() + " graph api status code ok..");
            //    string content = await response.Content.ReadAsStringAsync();
            //    System.IO.File.AppendAllText(path, Environment.NewLine + System.DateTime.Now.ToString() +  content);
            //}
            //else
            //{
            //    System.IO.File.AppendAllText(path, Environment.NewLine + System.DateTime.Now.ToString() + " graph api status code failed..");
            //}

            var     fb     = new Facebook.FacebookClient();
            dynamic result = fb.Post("oauth/access_token", new
            {
                client_id     = client.AppId,
                client_secret = client.AppSecret,
                redirect_uri  = RedirectUri.AbsoluteUri,
                code          = code
            });


            var accessToken = result.access_token;

            if (writeLog)
            {
                System.IO.File.AppendAllText(path, Environment.NewLine + System.DateTime.Now.ToString() + accessToken);
            }
            fb.AccessToken = accessToken;
            dynamic me = fb.Get("me?fields=first_name,last_name,id,email");

            ViewBag.EmailError = false;
            if (string.IsNullOrEmpty(me.email) == true)
            {
                if (writeLog)
                {
                    System.IO.File.AppendAllText(path, Environment.NewLine + System.DateTime.Now.ToString() + " fb email not found...");
                }

                ViewBag.EmailError = true;
                return(View("CallBack"));
            }

            var    email     = me.email;
            string firstname = me.first_name;
            string lastname  = me.last_name;
            var    id        = me.id;

            if (writeLog)
            {
                System.IO.File.AppendAllText(path, Environment.NewLine + System.DateTime.Now.ToString() + "found email and names...");
            }

            User user = await this._authenticationRepository.FindAsync(new Microsoft.AspNet.Identity.UserLoginInfo("Facebook", id));

            bool hasRegistered = user != null;

            if (writeLog)
            {
                System.IO.File.AppendAllText(path, Environment.NewLine + System.DateTime.Now.ToString() + " has registered.." + hasRegistered.ToString());
            }
            ViewBag.haslocalaccount = hasRegistered.ToString();

            bool hasCervitUser        = false;
            long existingCervitUserId = this._authenticationRepository.FindUserExists(email);

            hasCervitUser = (existingCervitUserId > 0);
            if (hasRegistered)
            {
                hasCervitUser = false;
            }

            if (writeLog)
            {
                System.IO.File.AppendAllText(path, Environment.NewLine + System.DateTime.Now.ToString() + " has hasCervitUser.." + hasCervitUser.ToString());
            }
            ViewBag.hascervituser         = hasCervitUser.ToString();
            ViewBag.provider              = "Facebook";
            ViewBag.external_user_name    = id;
            ViewBag.external_access_token = accessToken;
            ViewBag.email = email;
            return(View("CallBack"));
        }
Ejemplo n.º 3
0
        public void ConfigureOAuth(IAppBuilder app)
        {
            app.UseExternalSignInCookie(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);

            OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
            OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
            {
                AllowInsecureHttp         = true,
                TokenEndpointPath         = new PathString("/token"),
                AuthorizeEndpointPath     = new PathString("/api/Account/ExternalLogin"),
                AccessTokenExpireTimeSpan = TimeSpan.FromHours(24),
                Provider             = new SimpleAuthorizationServerProvider(),
                RefreshTokenProvider = new SimpleRefreshTokenProvider()
            };

            // Token Generation
            app.UseOAuthAuthorizationServer(OAuthServerOptions);
            app.UseOAuthBearerAuthentication(OAuthBearerOptions);

            KaribouAlpha.DAL.KaribouAlphaContext db = new DAL.KaribouAlphaContext();

            LinkedInAuthClient linkedInAuthClient = db.LinkedInAuthClients.SingleOrDefault(_linked => _linked.Active);

            if (linkedInAuthClient != null)
            {
                ILinkedInAuthenticationProvider providerLnk = new KaribouAlpha.Authentication.LinkedInAuthenticationProvider();
                LinkedInAuthenticationOptions = new LinkedInAuthenticationOptions()
                {
                    ClientId     = linkedInAuthClient.ClientId,
                    ClientSecret = linkedInAuthClient.ClientSecret,
                    Provider     = providerLnk,
                    CallbackPath = new PathString("/AuthCallBack."),
                    Scope        = { "r_basicprofile", "r_emailaddress" },
                    //BackchannelHttpHandler = new LinkedInBackChannelHandler()
                };
            }
            //http://www.c-sharpcorner.com/article/implementing-oauth2-0-authorization-for-google-in-asp-net/
            //https://developers.google.com/actions/identity/oauth2-code-flow

            GoogleAuthClient googleClient = db.GoogleAuthClients.SingleOrDefault(_google => _google.Active);

            if (googleClient != null)
            {
                GoogleAuthProvider gProvider = new GoogleAuthProvider();
                googleAuthOptions = new GoogleOAuth2AuthenticationOptions()
                {
                    ClientId     = googleClient.ClientId,
                    ClientSecret = googleClient.ClientSecret,
                    Provider     = gProvider
                };
            }

            KaribouAlpha.Models.FaceBookClient clientFb = db.FaceBookClients.SingleOrDefault(_fb => _fb.Active);
            if (clientFb != null)
            {
                var fbProvider          = new FacebookAuthProvider();
                var facebookAuthOptions = new FacebookAuthenticationOptions()
                {
                    AppId     = clientFb.AppId,
                    AppSecret = clientFb.AppSecret,
                    Provider  = fbProvider,
                };
                app.UseFacebookAuthentication(facebookAuthOptions);
            }
        }