public bool CheckSecurity()
 {
     if (Username == null)
     {
         LoginRequired?.Invoke(this, new LoginEventArg());
         //wait logic here
     }
     return(CheckAccess());
 }
Exemple #2
0
        public void Logout()
        {
            if (!this.Server.IsConnected)
            {
                return;
            }

            this.Server.Disconnect(10000, true);

            ClearStoredToken();

            // Сообщаем о том что вновнь нужен логин

            LoginRequired?.Invoke(this, EventArgs.Empty);
        }
Exemple #3
0
        /// <summary>
        /// Подключается к серверу с использованием токена аутентификации.
        /// </summary>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="token"></param>
        /// <param name="acceptSsl"></param>
        /// <returns></returns>
        public async Task ConnectToken(string connectionUri, string token)
        {
            var loginInfo = new Lers.Networking.TokenAuthenticationInfo(token);

            try
            {
                var uri = new Uri(connectionUri);
                await this.Server.ConnectAsync(uri, null, loginInfo, CancellationToken.None);
            }
            catch (Lers.Networking.AuthorizationFailedException)
            {
                // Произошла ошибка аутентификации. Нужно очистить токен и сообщить что требуется логин.

                ClearStoredToken();

                LoginRequired?.Invoke(this, EventArgs.Empty);
            }
        }
        /// <summary>
        /// dispatcher will fire corresponding command according to the incoming URL
        /// </summary>
        /// <param name="command">request command by URL</param>
        /// <returns></returns>
        public IResult Execute(string command)
        {
            string   strCommand = GetCommand(command);
            ICommand iCommand   = null;
            Result   comResult  = new Result();

            comResult.Success = false;
            IResult oResult = ObjectBuilder.CreateFrom(WebShellConfig.GetCommandType(strCommand));

            if (oResult.Success)
            {
                iCommand = oResult.Data as ICommand;

                bool          isValidUser   = false;
                object[]      oArr          = iCommand.GetType().GetCustomAttributes(typeof(LoginRequired), true);
                LoginRequired loginRequired = null;
                if (oArr.Length > 0)
                {
                    loginRequired = oArr[0] as LoginRequired;
                    if (loginRequired.Active == true)
                    {
                        ISecurity iSecurity = ObjectBuilder.CreateFrom(WebShellConfig.GetCommandType("security")).Data as ISecurity;
                        isValidUser = iSecurity.IsValidUser();
                    }
                    else
                    {
                        isValidUser = true;
                    }
                }
                else
                {
                    isValidUser = true;
                }

                if (isValidUser)
                {
                    command = command.Remove(0, strCommand.Length);
                    if (command.StartsWith("/"))
                    {
                        command = command.Remove(0, 1);
                    }
                    comResult = iCommand.Execute(command) as Result;
                }
                else if (!isValidUser && loginRequired != null)
                {
                    string message = "try to access \"login required\" form \r\n command url:" + HttpContext.Current.Request.RawUrl;
                    WebShell.Utilities.Log.Write(this.ToString(), "not authorized user", message);
                    if (loginRequired.RedirectTo != null)
                    {
                        HttpContext.Current.Response.Redirect(AppData.GetBaseUrl() + "security/login/?r=" + loginRequired.RedirectTo);
                    }
                    else
                    {
                        HttpContext.Current.Response.Redirect(AppData.GetBaseUrl() + "security/login/?r=" + command);
                    }
                }
                else
                {
                    //may be not reachable
                    comResult.Data = "You are not authorized user";
                    WebShell.Utilities.Log.Write(this.ToString(), "not autorized user", "command url:" + HttpContext.Current.Request.RawUrl);
                }
            }


            return(comResult);
        }