Exemple #1
0
        public virtual ActionResult LoginRedirect()
        {
            // Get the details of the currently logged in user.
            ShibbolethToken token = ShibbolethToken.CreateCurrent();

            // Integrity check.
            if (token == null)
            {
                throw new InvalidOperationException("The Shibboleth token is required!");
            }

            // Create the view-model.
            LoginRedirectVM model = new LoginRedirectVM();

            // Validate that the current user has her e-mail address specified, because it is required for the ELMS registration.
            // If the e-mail address is missing, then display a specific error message.
            if (String.IsNullOrEmpty(token.Email))
            {
                model.FailureReason = ElmsRegistrationFailureReason.MissingEmail;
                return(this.View(Views.LoginRedirect, model));
            }

            // Try to register the user in the ELMS WebStore.
            ElmsRegistrationResult result = ElmsHelper.Register(token);

            // If the registration failed, display a specific error message.
            if (!result.IsSuccess)
            {
                model.FailureReason = result.StatusCode == HttpStatusCode.BadRequest
                                        ? ElmsRegistrationFailureReason.ClientError
                                        : ElmsRegistrationFailureReason.ServiceError;

                return(this.View(Views.LoginRedirect, model));
            }

            // Redirect the user to the ELMS WebStore only if the registration was successful.
            // Integrity check: the redirect URL must be specified.
            if (result.RedirectUri == null)
            {
                throw new InvalidOperationException("The redirect URL must be specified!");
            }

            // If the redirect URL is specified, then redirect the registered user to the ELMS WebStore.
            return(this.Redirect(result.RedirectUri.ToString()));
        }
Exemple #2
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);
        }