Exemple #1
0
        public static ActionResult ValidateCredentials(Session session)
        {
            // Encrypt token.
            if (session["REGISTRAR_TOKEN"].Length > 0 && !session["REGISTRAR_TOKEN"].StartsWith("ENCRYPTED:"))
            {
                session["REGISTRAR_TOKEN"] = "ENCRYPTED:" + Convert.ToBase64String(ProtectedData.Protect(
                                                                                       Encoding.ASCII.GetBytes(session["REGISTRAR_TOKEN"]),
                                                                                       null, DataProtectionScope.LocalMachine
                                                                                       ));
            }

            // Validate dialog fields.
            try {
                if (session["REGISTRAR_REGISTRAR"].Length == 0)
                {
                    throw new QueryAPIException(103);
                }
                using (IQueryAPI api = QueryAPIIndex.I.Factory(session["REGISTRAR_REGISTRAR"])) {
                    // Testing for Error98 in the above using statement.
                    api.Credentials(session["REGISTRAR_USER"], session["REGISTRAR_TOKEN"].Replace("ENCRYPTED:", ""),
                                    session["REGISTRAR_DOMAIN"]
                                    );
                    if (api.UserLength == 0)
                    {
                        throw new QueryAPIException(100);
                    }
                    if (api.TokenLength == 0)
                    {
                        throw new QueryAPIException(101);
                    }
                    if (api.DomainLength == 0)
                    {
                        throw new QueryAPIException(102);
                    }
                }
            } catch (QueryAPIException err) {
                using (TaskDialog dialog = new TaskDialog()) {
                    // Launch the dialog and get result.
                    Thread thread = new Thread((ThreadStart) delegate { dialog.ShowError(Strings.ErrorDialogTitle, err.RMessage + "\n"); });
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                    thread.Join();
                    session["_RegistrarValidated"] = "0";
                    return(ActionResult.NotExecuted);
                }
            }

            // Validate with status dialog.
            using (IQueryAPI api = QueryAPIIndex.I.Factory(session["REGISTRAR_REGISTRAR"]))
                using (StatusDialog dialog = new StatusDialog(api)) {
                    // Pass credentials to class instance.
                    api.Credentials(session["REGISTRAR_USER"], session["REGISTRAR_TOKEN"].Replace("ENCRYPTED:", ""),
                                    session["REGISTRAR_DOMAIN"]
                                    );

                    // Launch the dialog and get result.
                    Thread thread = new Thread((ThreadStart) delegate { dialog.Show(); });
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                    thread.Join();
                    if (dialog.Result == TaskDialog.TaskDialogResult.Ok)
                    {
                        session["_RegistrarValidated"] = "1";
                        return(ActionResult.Success);
                    }
                    else
                    {
                        session["_RegistrarValidated"] = "0";
                        return(ActionResult.NotExecuted);
                    }
                }
        }
        public static ActionResult ValidateCredentials( Session session )
        {
            // Encrypt token.
            if ( session["REGISTRAR_TOKEN"].Length > 0 && !session["REGISTRAR_TOKEN"].StartsWith("ENCRYPTED:") ) {
                session["REGISTRAR_TOKEN"] = "ENCRYPTED:" + Convert.ToBase64String( ProtectedData.Protect(
                    Encoding.ASCII.GetBytes( session["REGISTRAR_TOKEN"] ),
                    null, DataProtectionScope.LocalMachine
                    ) );
            }

            // Validate dialog fields.
            try {
                if ( session["REGISTRAR_REGISTRAR"].Length == 0 ) throw new QueryAPIException( 103 );
                using ( IQueryAPI api = QueryAPIIndex.I.Factory( session["REGISTRAR_REGISTRAR"] ) ) {
                    // Testing for Error98 in the above using statement.
                    api.Credentials( session["REGISTRAR_USER"], session["REGISTRAR_TOKEN"].Replace( "ENCRYPTED:", "" ),
                        session["REGISTRAR_DOMAIN"]
                        );
                    if ( api.UserLength == 0 ) throw new QueryAPIException( 100 );
                    if ( api.TokenLength == 0 ) throw new QueryAPIException( 101 );
                    if ( api.DomainLength == 0 ) throw new QueryAPIException( 102 );
                }
            } catch ( QueryAPIException err ) {
                using ( TaskDialog dialog = new TaskDialog() ) {
                    // Launch the dialog and get result.
                    Thread thread = new Thread( (ThreadStart) delegate { dialog.ShowError( Strings.ErrorDialogTitle, err.RMessage + "\n" ); } );
                    thread.SetApartmentState( ApartmentState.STA );
                    thread.Start();
                    thread.Join();
                    session["_RegistrarValidated"] = "0";
                    return ActionResult.NotExecuted;
                }
            }

            // Validate with status dialog.
            using ( IQueryAPI api = QueryAPIIndex.I.Factory( session["REGISTRAR_REGISTRAR"] ) )
            using ( StatusDialog dialog = new StatusDialog( api ) ) {
                // Pass credentials to class instance.
                api.Credentials( session["REGISTRAR_USER"], session["REGISTRAR_TOKEN"].Replace( "ENCRYPTED:", "" ),
                    session["REGISTRAR_DOMAIN"]
                    );

                // Launch the dialog and get result.
                Thread thread = new Thread( (ThreadStart) delegate { dialog.Show(); } );
                thread.SetApartmentState( ApartmentState.STA );
                thread.Start();
                thread.Join();
                if ( dialog.Result == TaskDialog.TaskDialogResult.Ok ) {
                    session["_RegistrarValidated"] = "1";
                    return ActionResult.Success;
                } else {
                    session["_RegistrarValidated"] = "0";
                    return ActionResult.NotExecuted;
                }
            }
        }