Exemplo n.º 1
0
        public void Logout()
        {
            DynamicDictionary authDetail = null;

            if (SessionData.token != null)
            {
                if (SessionData.user_id != null)
                {
                    authDetail = GetTokenDetail(SessionData.token, (int)SessionData.user_id);
                }
            }
            if (authDetail != null)
            {
                int?session_id = authDetail.GetValueAsInt("id");
                //Delete(session_id);
                SessionLogService srvc = new SessionLogService();
                srvc.Delete(session_id);
            }
            if (HttpContext.Current.Response.Cookies.AllKeys.Contains("token"))
            {
                HttpContext.Current.Response.Cookies.Remove("token");
            }
            if (HttpContext.Current.Response.Cookies.AllKeys.Contains("user_id"))
            {
                HttpContext.Current.Response.Cookies.Remove("user_id");
            }
            SessionData.Session.Clear();
        }
Exemplo n.º 2
0
        public DynamicDictionary ValidateToken(DbConnect con, string token, int user_id)
        {
            DynamicDictionary data        = new DynamicDictionary();
            SessionLogService sessionSrvc = new SessionLogService();

            DynamicDictionary data_param = new DynamicDictionary();

            data_param.Add("token", token);
            data_param.Add("user_id", user_id);
            data = sessionSrvc.CrudRepo.Get(con, data_param);

            return(data);
        }
Exemplo n.º 3
0
        public virtual ResponseAuth Authenticate(int client_id, string username, string password)
        {
            //validate if data is passed with all 3 parameters.
            ResponseAuth resp = new ResponseAuth();

            if (client_id < 1)
            {
                resp.message = "Please select a Client.";
                return(resp);
            }
            if (username.Trim().Length == 0)
            {
                resp.message = "Please enter a Username.";
                return(resp);
            }
            if (password.Trim().Length == 0)
            {
                resp.message = "Please enter a Password.";
                return(resp);
            }
            //var usermodel = Bango.Container.GetInstance<IUserModel>();
            //UserService<UserModel, int?> userSrvc = (UserService<UserModel, int?>)Bango.Container.GetInstance(typeof(IUserService<UserModel, int?>));
            var userSrvc = GetUserServiceInstance();

            using (DbConnect con = new DbConnect())
            {
                resp = userSrvc.AuthenticateUserNamePasword(con, client_id, username, password);
                //generate token
                string token = string.Empty;
                if (resp.success)
                {
                    token = GenerateToken();
                }
                resp.token = token;
                //save data in session & generate
                SessionLogService sessionSrvc = new SessionLogService();
                DynamicDictionary data_param  = new DynamicDictionary();
                data_param.Add("client_id", client_id);
                data_param.Add("user_id", resp.user_id);
                DateTime login_datetime = DateTime.Now;
                data_param.Add("login_datetime", login_datetime);
                data_param.Add("expire_datetime", GetExpirtyDateTime(login_datetime));
                data_param.Add("token", token);

                sessionSrvc.Insert(con, data_param);
                //SessionLogModel
            }

            return(resp);
        }
        protected bool AuthenticationFromDB(HttpActionContext actionContext, string token, int user_id)
        {
            if (!App.CheckToken)
            {
                return(true);
            }
            IAuthService      authSrvc    = App.Container.GetInstance <Rbac.IAuthService>();
            DynamicDictionary tokenDetail = authSrvc.GetTokenDetail(token, user_id);

            if (tokenDetail == null || tokenDetail.GetCount() == 0)
            {
                Status = AuthorizationStatus.NotLoggedIn;
                return(false);
            }
            if (tokenDetail.ContainsKey("expire_datetime"))
            {
                if (!String.IsNullOrEmpty(tokenDetail["expire_datetime"].ToString()))
                {
                    DateTime expiryDate   = Convert.ToDateTime(tokenDetail["expire_datetime"]);
                    DateTime current_date = DateTime.Now;
                    TimeSpan difference   = expiryDate - current_date;
                    if (difference.TotalMinutes < 0)
                    {
                        Status = AuthorizationStatus.SessionExpired;
                        return(false);
                    }
                    else
                    {
                        int?session_id = tokenDetail.GetValueAsInt("id");
                        _client_id            = (int)tokenDetail.GetValueAsInt("client_id");
                        SessionData.client_id = _client_id;
                        DynamicDictionary data_param = new DynamicDictionary();
                        data_param.Add("expire_datetime", AuthService.GetExpirtyDateTime(DateTime.Now));
                        data_param.Add("id", session_id);
                        SessionLogService logSrvc = new SessionLogService();
                        logSrvc.Update(session_id, data_param);
                        Status = AuthorizationStatus.Authorized;
                        return(true);
                    }
                }
            }
            return(false);
        }