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"];
            }
            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);
        }
        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);

            state.AddError(authResult.Error != null
                                ? authResult.Error.Message
                                : _services.Localization.GetResource("Admin.Common.UnknownError"));

            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"];
			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);
        }
Пример #5
0
        private AuthorizeState VerifyAuthentication(string returnUrl)
        {
            string error = null;
            AuthenticationResult authResult = null;

            try
            {
                authResult = this.FacebookApplication.VerifyAuthentication(_httpContext, GenerateLocalCallbackUri());
            }
            catch (WebException wexc)
            {
                using (var response = wexc.Response as HttpWebResponse)
                {
                    error = response.StatusDescription;

                    var enc = Encoding.GetEncoding(response.CharacterSet);
                    using (var reader = new StreamReader(response.GetResponseStream(), enc))
                    {
                        var rawResponse = reader.ReadToEnd();
                        Logger.Log(LogLevel.Error, new Exception(rawResponse), response.StatusDescription, null);
                    }
                }
            }
            catch (Exception exception)
            {
                error = exception.ToString();
                Logger.Log(LogLevel.Error, exception, null, null);
            }

            if (authResult != null && 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(FacebookExternalAuthMethod.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));
            }

            if (error.IsEmpty() && authResult != null && authResult.Error != null)
            {
                error = authResult.Error.Message;
            }
            if (error.IsEmpty())
            {
                error = _services.Localization.GetResource("Admin.Common.UnknownError");
            }

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

            state.AddError(error);

            return(state);
        }