private AuthorizeState VerifyAuthentication(string returnUrl)
        {
            var authResult = this.FacebookApplication.VerifyAuthentication(_httpContext, GenerateLocalCallbackUri());

            if (authResult.IsSuccessful)
            {
                if (!authResult.ExtraData.ContainsKey("id"))
                    throw new Exception("Authentication result does not contain id data");

                if (!authResult.ExtraData.ContainsKey("accesstoken"))
                    throw new Exception("Authentication result does not contain accesstoken data");

                var parameters = new OAuthAuthenticationParameters(Provider.SystemName)
                {
                    ExternalIdentifier = authResult.ProviderUserId,
                    OAuthToken = authResult.ExtraData["accesstoken"],
                    OAuthAccessToken = authResult.ProviderUserId,
                };

                if (_externalAuthenticationSettings.AutoRegisterEnabled)
                    ParseClaims(authResult, parameters);

                var result = _authorizer.Authorize(parameters);

                return new AuthorizeState(returnUrl, result);
            }

            var state = new AuthorizeState(returnUrl, OpenAuthenticationStatus.Error);
            var error = authResult.Error != null ? authResult.Error.Message : "Unknown error";
            state.AddError(error);
            return state;
        }
        private void ParseClaims(AuthenticationResult authenticationResult, OAuthAuthenticationParameters parameters)
        {
            var claims = new UserClaims();

            claims.Contact = new ContactClaims();
            if (authenticationResult.ExtraData.ContainsKey("username"))
            {
                claims.Contact.Email = authenticationResult.ExtraData["username"];
            }
            else
            {
                //request email
                claims.Contact.Email = RequestEmailFromFacebook(authenticationResult.ExtraData["accesstoken"]);
            }
            claims.Name = new NameClaims();
            if (authenticationResult.ExtraData.ContainsKey("name"))
            {
                var name = authenticationResult.ExtraData["name"];
                if (!String.IsNullOrEmpty(name))
                {
                    var nameSplit = name.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (nameSplit.Length >= 2)
                    {
                        claims.Name.First = nameSplit[0];
                        claims.Name.Last  = nameSplit[1];
                    }
                    else
                    {
                        claims.Name.Last = nameSplit[0];
                    }
                }
            }

            parameters.AddClaim(claims);
        }
        private void ParseClaims(AuthenticationResult authenticationResult, OAuthAuthenticationParameters parameters)
        {
            var claims = new UserClaims();
            claims.Contact = new ContactClaims();
            if (authenticationResult.ExtraData.ContainsKey("username"))
                claims.Contact.Email = authenticationResult.ExtraData["username"];
            claims.Name = new NameClaims();
            if (authenticationResult.ExtraData.ContainsKey("name"))
            {
                var name = authenticationResult.ExtraData["name"];
                if (!String.IsNullOrEmpty(name))
                {
                    var nameSplit = name.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (nameSplit.Length >= 2)
                    {
                        claims.Name.First = nameSplit[0];
                        claims.Name.Last = nameSplit[1];
                    }
                    else
                    {
                        claims.Name.Last = nameSplit[0];
                    }
                }
            }

            parameters.AddClaim(claims);
        }
Exemplo n.º 4
0
        private AuthorizeState TranslateResponseState(string returnUrl, FacebookOAuthResult oAuthResult)
        {
            if (oAuthResult.IsSuccess)
            {
                var accessToken = GetAccessToken(oAuthResult.Code);
                var parameters  = new OAuthAuthenticationParameters(Provider.SystemName)
                {
                    ExternalIdentifier = accessToken,
                    OAuthToken         = oAuthResult.Code,
                    OAuthAccessToken   = accessToken
                };

                if (_externalAuthenticationSettings.AutoRegisterEnabled)
                {
                    GetClaims(parameters);
                }

                var result = _authorizer.Authorize(parameters);

                return(new AuthorizeState(returnUrl, result));
            }

            var state = new AuthorizeState(returnUrl, OpenAuthenticationStatus.Error);

            state.AddError(string.Format("Reason: {0}, Description: {1}", oAuthResult.ErrorReason, oAuthResult.ErrorDescription));
            return(state);
        }
Exemplo n.º 5
0
        private void GetClaims(OAuthAuthenticationParameters parameters)
        {
            var client = new FacebookClient(parameters.OAuthAccessToken);
            var me     = client.Get("/me");

            var claimsTranslator = new FacebookClaimsTranslator();
            var claims           = claimsTranslator.Translate((IDictionary <string, object>)me);

            parameters.AddClaim(claims);
        }
 public FacebookClient GetClient(Customer customer)
 {
     var parameters = new OAuthAuthenticationParameters(Provider.SystemName);
     var identifier = _openAuthenticationService
         .GetExternalIdentifiersFor(customer)
         .FirstOrDefault(o => o.ProviderSystemName == parameters.ProviderSystemName);
     if (identifier== null)
         throw new Exception("No identifier could be loaded");
     return !string.IsNullOrEmpty(identifier.OAuthAccessToken) ? new FacebookClient(identifier.OAuthAccessToken) : null;
 }
        public FacebookClient GetClient(Customer customer)
        {
            var parameters = new OAuthAuthenticationParameters(Provider.SystemName);
            var identifier = _openAuthenticationService
                .GetExternalIdentifiersFor(customer)
                .Where(o => o.ProviderSystemName == parameters.ProviderSystemName)
                .ToList()
                .FirstOrDefault();

            return !string.IsNullOrEmpty(identifier.OAuthAccessToken) ? new FacebookClient(identifier.OAuthAccessToken) : null;
        }
Exemplo n.º 8
0
        public FacebookClient GetClient(Customer customer)
        {
            var parameters = new OAuthAuthenticationParameters(Provider.SystemName);
            var identifier = _openAuthenticationService
                             .GetExternalIdentifiersFor(customer)
                             .Where(o => o.ProviderSystemName == parameters.ProviderSystemName)
                             .ToList()
                             .FirstOrDefault();

            return(!string.IsNullOrEmpty(identifier.OAuthAccessToken) ? new FacebookClient(identifier.OAuthAccessToken) : null);
        }
        public FacebookClient GetClient(Customer customer)
        {
            var parameters = new OAuthAuthenticationParameters(Provider.SystemName);
            var identifier = _openAuthenticationService
                             .GetExternalIdentifiersFor(customer)
                             .FirstOrDefault(o => o.ProviderSystemName == parameters.ProviderSystemName);

            if (identifier == null)
            {
                throw new Exception("No identifier could be loaded");
            }
            return(!string.IsNullOrEmpty(identifier.OAuthAccessToken) ? new FacebookClient(identifier.OAuthAccessToken) : null);
        }
        private AuthorizeState VerifyAuthentication(string returnUrl)
        {
            var authResult = this.FacebookApplication.VerifyAuthentication(_httpContext, GenerateLocalCallbackUri());

            if (authResult.IsSuccessful)
            {
                if (!authResult.ExtraData.ContainsKey("id"))
                {
                    throw new Exception("Authentication result does not contain id data");
                }

                if (!authResult.ExtraData.ContainsKey("accesstoken"))
                {
                    throw new Exception("Authentication result does not contain accesstoken data");
                }

                var parameters = new OAuthAuthenticationParameters(Provider.SystemName)
                {
                    ExternalIdentifier = authResult.ProviderUserId,
                    OAuthToken         = authResult.ExtraData["accesstoken"],
                    OAuthAccessToken   = authResult.ProviderUserId,
                };

                if (_externalAuthenticationSettings.AutoRegisterEnabled)
                {
                    ParseClaims(authResult, parameters);
                }

                var result = _authorizer.Authorize(parameters);

                return(new AuthorizeState(returnUrl, result));
            }

            var state = new AuthorizeState(returnUrl, OpenAuthenticationStatus.Error);
            var error = authResult.Error != null ? authResult.Error.Message : "Unknown error";

            state.AddError(error);
            return(state);
        }
        private AuthorizeState TranslateResponseState(string returnUrl, FacebookOAuthResult oAuthResult)
        {
            if (oAuthResult.IsSuccess)
            {
                var parameters = new OAuthAuthenticationParameters(Provider.SystemName)
                {
                    ExternalIdentifier = GetAccessToken(oAuthResult.Code),
                    OAuthToken = oAuthResult.Code,
                    OAuthAccessToken = GetAccessToken(oAuthResult.Code)
                };

                if (_externalAuthenticationSettings.AutoRegisterEnabled)
                    GetClaims(parameters);

                var result = _authorizer.Authorize(parameters);

                return new AuthorizeState(returnUrl, result);
            }

            var state = new AuthorizeState(returnUrl, OpenAuthenticationStatus.Error);
            state.AddError(string.Format("Reason: {0}, Description: {1}", oAuthResult.ErrorReason, oAuthResult.ErrorDescription));
            return state;
        }
        private void GetClaims(OAuthAuthenticationParameters parameters)
        {
            var client = new FacebookClient(parameters.OAuthAccessToken);
            var me = client.Get("/me");

            var claimsTranslator = new FacebookClaimsTranslator();
            var claims = claimsTranslator.Translate((IDictionary<string, object>)me);

            parameters.AddClaim(claims);
        }