Beispiel #1
0
        /// <summary>
        /// Creates a new <see cref="ShibbolethToken"/> from the current values of the HTTP server variables.
        /// </summary>
        /// <returns></returns>
        public static ShibbolethToken CreateCurrent()
        {
            // Get all server variables.
            NameValueCollection vars = HttpContext.Current.Request.ServerVariables;

            // Create a new token and set its properties using the attached ServerVariable attributes.
            ShibbolethToken token = new ShibbolethToken();

            PropertyInfo[] properties = token.GetType().GetProperties();
            foreach (PropertyInfo property in properties)
            {
                ServerVariableAttribute attr = property.GetCustomAttribute <ServerVariableAttribute>();
                if (attr != null)
                {
                    string value = vars[attr.Name];
                    if (!String.IsNullOrEmpty(value))
                    {
                        property.SetValue(token, value);
                    }
                }
            }

            return(token);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new <see cref="ShibbolethToken"/> from the current values of the HTTP server variables.
        /// </summary>
        /// <returns></returns>
        public static ShibbolethToken CreateCurrent()
        {
            // Get all server variables.
            NameValueCollection vars = HttpContext.Current.Request.ServerVariables;

            // Create a new token and set its properties using the attached ServerVariable attributes.
            ShibbolethToken token = new ShibbolethToken();
            PropertyInfo[] properties = token.GetType().GetProperties();
            foreach( PropertyInfo property in properties )
            {
                ServerVariableAttribute attr = property.GetCustomAttribute<ServerVariableAttribute>();
                if( attr != null )
                {
                    string value = vars[ attr.Name ];
                    if( !String.IsNullOrEmpty( value ) )
                    {
                        property.SetValue( token, value );
                    }
                }
            }

            return token;
        }
Beispiel #3
0
        public static ElmsRegistrationResult Register( ShibbolethToken token )
        {
            // Input validation.
            if( token == null )
            {
                Log.WriteError( "ElmsHelper.Register: token is null!" );
                throw new ArgumentNullException( "token" );
            }

            if( String.IsNullOrEmpty( token.Email ) )
            {
                Log.WriteError( "ElmsHelper.Register: token.Email is null!" );
                throw new ArgumentException( "The e-mail address is required for the registration!" );
            }

            // Prepare the constant parameters.
            string accountNumber = ConfigHelper.Elms.AccountNumber;
            string key = ConfigHelper.Elms.Key;

            // Prepare the user-specific parameters.
            string userName = HttpUtility.UrlEncode( token.Email );
            string email = HttpUtility.UrlEncode( token.Email );

            // Prepare the parameter that describe the user's permissions.
            List<string> statusList = new List<string>();
            if( token.IsStudent )
            {
                statusList.Add( ElmsHelper.StudentStatus );
            }
            if( token.IsSupervisor )
            {
                statusList.Add( ElmsHelper.StaffStatus );
                statusList.Add( ElmsHelper.FacultyStatus );
            }
            string statuses = String.Join( ",", statusList );

            // Build the request URL.
            string serviceUrl = String.Format( CultureInfo.InvariantCulture, "{0}?account={1}&key={2}&username={3}&academic_statuses={4}&email={5}", ElmsHelper.RegistrationUrl, accountNumber, key, userName, statuses, email );

            // Integrity check: the built URL must be a valid URI.
            Uri serviceUri;
            if( !Uri.TryCreate( serviceUrl, UriKind.Absolute, out serviceUri ) )
            {
                Log.WriteError( "ElmsHelper.Register: Invalid ServiceURL: {0}", serviceUrl );
                throw new InvalidOperationException( "The service URL is invalid!" );
            }

            // Send the HTTP GET request to the ELMS WebStore.
            ElmsRegistrationResult result = new ElmsRegistrationResult();
            try
            {
                // NOTE: This low-level class must be used, becuase the WebClient class does not provide access to the HTTP status code.
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create( serviceUri );
                using( HttpWebResponse response = (HttpWebResponse) request.GetResponse() )
                {
                    result.StatusCode = response.StatusCode;

                    // Check the HTTP status code.
                    if( response.StatusCode == HttpStatusCode.OK )
                    {
                        using( StreamReader reader = new StreamReader( response.GetResponseStream() ) )
                        {
                            // Read the raw content from the HTTP response.
                            string responseText = reader.ReadToEnd();

                            // Check that the raw content of the HTTP response is a valid absolute URL.
                            Uri responseUri;
                            if( Uri.TryCreate( responseText, UriKind.Absolute, out responseUri ) )
                            {
                                result.IsSuccess = true;
                                result.RedirectUri = responseUri;
                            }
                        }
                    }
                }
            }
            catch( WebException ex )
            {
                // Log the error.
                Log.WriteError( ex );
                Log.WriteError( "ElmsHelper.Register: WebException! ServiceURL: {0}", serviceUrl );

                HttpWebResponse response = ex.Response as HttpWebResponse;
                if( response != null )
                {
                    Log.WriteError( "ElmsHelper.Register: WebException! StatusCode: {0}", response.StatusCode );
                    Log.WriteError( "ElmsHelper.Register: WebException! StatusDescription: {0}", response.StatusDescription );
                }

                // Treat the registration unsuccessful in case of any error.
                result.IsSuccess = false;
            }
            catch( Exception ex )
            {
                // Log the error.
                Log.WriteException( ex );

                // Treat the registration unsuccessful in case of any error.
                result.IsSuccess = false;
            }

            return result;
        }