示例#1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var result = Request["result"];

            if (result.Length % 4 == 0)
            {
                result = SignHandler.Base64Decode(result);
            }
            if ("ok" == result.ToLower())
            {
                try
                {
                    String logonto = ConfigurationManager.AppSettings["logonto"];
                    if (logonto == null || "".Equals(logonto.Trim()))
                    {
                        throw new ConfigurationErrorsException("Error - logonto parameter from configuration is missing or empty");
                    }
                    string signature = SignHandler.Base64Decode(Request["signature"]);
                    CertificateAndStatus certificateAndStatus = LogonHandler.ValidateAndExtractCertificateAndStatus(signature, Challenge(), logonto);
                    if (certificateAndStatus.Certificate is PocesCertificate || certificateAndStatus.Certificate is MocesCertificate)
                    {
                        CertificateStatus status = certificateAndStatus.CertificateStatus;
                        if (status == CertificateStatus.Valid)
                        {
                            SetAttributesForMocesOrPoces(certificateAndStatus.Certificate);

                            if (Session[Global.CurrentUser] == null)
                            {
                                var randomUserName = ChallengeGenerator.GenerateChallenge();
                                Session.Add(Global.CurrentUser, randomUserName);
                            }
                            if (!Roles.IsUserInRole((string)Session[Global.CurrentUser], "nemid"))
                            {
                                Roles.AddUserToRole((string)Session[Global.CurrentUser], "nemid");
                            }
                            FormsAuthentication.RedirectFromLoginPage((string)Session[Global.CurrentUser], false);
                        }
                        else
                        {
                            Session.Add("errorText", "Certifikatet er " + ErrorHandler.GetCertificateStatusText(status));
                        }
                    }
                    else
                    {
                        Session.Add("notPocesOrMoces", true);
                    }
                }
                catch (NonOcesCertificateException)
                {
                    Session.Add("errorText", "Ikke et OCES-certifikat");
                }
                catch (Exception)
                {
                    Session.Add("errorText", "Ukendt server-fejl");
                }
            }
            else
            {
                Session.Add("errorText", ErrorHandler.GetErrorText(result));
            }
        }
示例#2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var result = Request["result"];

            if (result.Length % 4 == 0)
            {
                result = SignHandler.Base64Decode(result);
            }
            if ("ok" == result.ToLower())
            {
                try
                {
                    String friendlyName = ConfigurationManager.AppSettings.Get("logonto");

                    if (friendlyName == null || "".Equals(friendlyName.Trim()))
                    {
                        throw new ConfigurationException("Fejl: Logonto / friendlyname mangler, eller er tom i konfigurationen");
                    }

                    string signature = SignHandler.Base64Decode(Request["signature"]);
                    CertificateAndStatus certificateAndStatus = LogonHandler.ValidateAndExtractCertificateAndStatus(signature, Challenge(), friendlyName);
                    if (certificateAndStatus.Certificate is PocesCertificate)
                    {
                        CertificateStatus status = certificateAndStatus.CertificateStatus;
                        if (status == CertificateStatus.Valid)
                        {
                            Session.Add(KeyPid, ((PocesCertificate)certificateAndStatus.Certificate).Pid);
                            if (Session[Global.CurrentUser] == null)
                            {
                                var randomUserName = ChallengeGenerator.GenerateChallenge();
                                Session.Add(Global.CurrentUser, randomUserName);
                            }

                            if (!Roles.IsUserInRole((string)Session[Global.CurrentUser], "poces"))
                            {
                                Roles.AddUserToRole((string)Session[Global.CurrentUser], "poces");
                            }
                            FormsAuthentication.RedirectFromLoginPage((string)Session[Global.CurrentUser], false);
                        }
                        else
                        {
                            Session.Add("errorText", "Certifikatet er " + ErrorHandler.GetCertificateStatusText(status));
                        }
                    }
                    else
                    {
                        Session.Add("notPoces", true);
                    }
                }
                catch (NonOcesCertificateException)
                {
                    Session.Add("errorText", "Ikke et OCES-certifikat");
                }
                catch (Exception ex)
                {
                    Session.Add("errorText", "Ukendt server-fejl: " + ex.Message);
                }
            }
            else
            {
                Session.Add("errorText", ErrorHandler.GetErrorText(result));
            }
        }
        /// <summary>
        /// Tring to recive token authorized in authority controller of target server.
        /// </summary>
        /// <param name="cancellationToken">Token that can be used to termination of the logon process.</param>
        /// <returns></returns>
        public bool TryToLogon(CancellationToken cancellationToken)
        {
            // Drop if already started.
            if (GuestTokenHandler.IsInProgress)
            {
                return(false);
            }

            bool asyncOperationStarted = false;

            #region Guest token processing
            // Is the guest token is relevant.
            bool guestTokenInvalid =
                string.IsNullOrEmpty(GuestTokenHandler.Token) ||
                Tokens.IsExpired(GuestTokenHandler.Token, GuestTokenHandler.ExpiryTime);

            if (guestTokenInvalid)
            {
                // Lock thread.
                asyncOperationStarted = true;

                // Callback that will be call when guest token would be recived.
                GuestTokenHandler.ProcessingFinished += GuestTokenRecivedCallback;
                void GuestTokenRecivedCallback(QueryProcessor _, bool result, object message)
                {
                    // Unsubscribe from handler.
                    GuestTokenHandler.ProcessingFinished -= GuestTokenRecivedCallback;

                    // Unlock thread.
                    asyncOperationStarted = false;
                }

                // Recive guest token to get access to server.
                GuestTokenHandler.TryToReciveTokenAsync(
                    routingIP,
                    pipeName,
                    cancellationToken);
            }

            // Wait for guest token.
            while (asyncOperationStarted)
            {
                Thread.Sleep(100);
            }

            // Drop if guest token not recived.
            if (string.IsNullOrEmpty(GuestTokenHandler.Token))
            {
                return(false);
            }
            #endregion

            #region Logon processing
            // Lock thread.
            asyncOperationStarted = true;

            // Callback that will be call whenlogon would be finished.
            LogonHandler.ProcessingFinished += LogonFinishedCallback;
            void LogonFinishedCallback(QueryProcessor _, bool result, object message)
            {
                // Unsubscribe from handler.
                LogonHandler.ProcessingFinished -= LogonFinishedCallback;

                // Unlock thread.
                asyncOperationStarted = false;
            }

            // Request logon.
            LogonHandler.TryToLogonAsync(
                GuestTokenHandler.Token,
                authLogin,
                authPassword,
                routingIP,
                pipeName);

            // Wait for guest token.
            while (asyncOperationStarted)
            {
                Thread.Sleep(100);
            }

            // Drop if guest token not recived.
            if (string.IsNullOrEmpty(LogonHandler.Token))
            {
                return(false);
            }
            #endregion

            return(true);
        }