Ejemplo n.º 1
0
        private async void LoginButtonClick(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(UserText.Text) && !string.IsNullOrEmpty(PassText.Text))
            {
                var username       = UserText.Text.ToLower();
                var api            = BuildApi(username, PassText.Text);
                var sessionHandler = new FileSessionHandler {
                    FilePath = username.GetAccountPath(), InstaApi = api
                };

                api.SessionHandler = sessionHandler;
                var loginResult = await api.LoginAsync();

                if (loginResult.Succeeded)
                {
                    LoggedInUsers.Add(api.GetLoggedUser().LoggedInUser.UserName.ToLower());
                    ApiList.Add(api);
                    if (SessionsCombo.SelectedIndex == -1)
                    {
                        SessionsCombo.SelectedIndex = 0;
                    }
                    api.SessionHandler.Save();
                }
                else
                {
                    MessageBox.Show($"Error:\r\n{loginResult.Info.Message}\r\n\r\n" +
                                    $"Please check ChallengeExample for handling two factor or challenge..."
                                    , loginResult.Info.ResponseType.ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
Ejemplo n.º 2
0
        protected override void OnBefore(IInvocation invocation)
        {
            _httpContextAccessor = ServiceTool.ServiceProvider.GetService <IHttpContextAccessor>();
            _loggedInUsers       = ServiceTool.ServiceProvider.GetService <LoggedInUsers>();

            var user = _httpContextAccessor.HttpContext.User;

            if (!user.Identity.IsAuthenticated)
            {
                throw new AuthenticationException(AspectMessage.AuthenticationError);
            }

            var accountId = user.GetAccountId();

            if (accountId == 0)
            {
                throw new AuthenticationException(AspectMessage.AuthenticationError);
            }

            var userInfo = _loggedInUsers.UserInfo.FirstOrDefault(x => x.AccountId == accountId);
            //if (userInfo == null)
            //    throw new SecurityException(AspectMessage.AccessDenied);

            var isSuperVisor = userInfo.AccountType == AccountType.SuperAdmin;

            if (isSuperVisor)
            {
                return;
            }

            throw new SecurityException(AspectMessage.AccessDenied);
        }
Ejemplo n.º 3
0
        public void InstallService(IServiceCollection services, IConfiguration configuration)
        {
            var jwtOptions = new JwtOptions();

            configuration.Bind(nameof(jwtOptions), jwtOptions);
            services.AddSingleton(jwtOptions);

            var loggedInUsers = new LoggedInUsers();

            configuration.Bind(nameof(loggedInUsers), loggedInUsers);
            services.AddSingleton(loggedInUsers);

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(x => {
                x.SaveToken = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidIssuer              = jwtOptions.Issuer,
                    ValidAudience            = jwtOptions.Audience,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SecurityKey)),
                    ClockSkew = TimeSpan.Zero
                };
            });
        }
Ejemplo n.º 4
0
        public AuthService(IDataAccessRepository <Account> dal, IMapper mapper, ITokenHelper tokenHelper, IUserService userService, LoggedInUsers loggedInUsers, JwtOptions jwtOptions)
        {
            _dal = dal;

            _tokenHelper   = tokenHelper;
            _userService   = userService;
            _loggedInUsers = loggedInUsers;
            _jwtOptions    = jwtOptions;
            _mapper        = mapper;
        }
Ejemplo n.º 5
0
        public AuthService(IDataAccessRepository <Personnel> dal, IDataAccessRepository <Rule> dalRule, ITokenHelper tokenHelper, IUserService userService, LoggedInUsers loggedInUsers, JwtOptions jwtOptions)
        {
            _dal     = dal;
            _dalRule = dalRule;

            _tokenHelper   = tokenHelper;
            _userService   = userService;
            _loggedInUsers = loggedInUsers;
            _jwtOptions    = jwtOptions;
        }
Ejemplo n.º 6
0
        public AuthService(IDataAccessRepository <User> dalUser, IDataAccessRepository <UserRole> dalUserRole, ITokenHelper tokenHelper, IActiveUserService activeUserService, LoggedInUsers loggedInUsers, JwtOptions jwtOptions)
        {
            _dalUser     = dalUser;
            _dalUserRole = dalUserRole;

            _tokenHelper       = tokenHelper;
            _activeUserService = activeUserService;
            _loggedInUsers     = loggedInUsers;
            _jwtOptions        = jwtOptions;
        }
            public static void AddUser(string connectionId, ModuleUser currentUser, int userId)
            {
                LoggedInUsers user = new LoggedInUsers();

                user.ConnectionId = connectionId;
                user.UserId       = userId;
                user.User         = currentUser;

                onLineUserList.Add(user);
            }
        public override async Task OnConnectedAsync()
        {
            var connectionId = Context.ConnectionId;
            var name         = Context.User.Identity.Name;
            var user         = await _context.ModuleUser.Where(u => u.Email == name).FirstOrDefaultAsync();

            if (user != null)
            {
                OnLineUser.AddUser(connectionId, user, user.UserId);

                var loggedUser = _context.LoggedInUsers.Where(u => u.UserId == user.UserId).FirstOrDefault();
                if (loggedUser != null)
                {
                    loggedUser.ConnectionId = connectionId;
                    _context.LoggedInUsers.Update(loggedUser);
                }
                else
                {
                    LoggedInUsers newLUser = new LoggedInUsers();
                    newLUser.ConnectionId = connectionId;
                    newLUser.UserId       = user.UserId;
                    _context.LoggedInUsers.AddRange(newLUser);
                }

                var userName = user.LastName + " " + user.FirstName;
                await Clients.Caller.SendAsync("onConnectedAsync", user.UserId, connectionId, userName, OnLineUser.onLineUserList);

                await Clients.AllExcept(connectionId).SendAsync("newUserConnected", user.UserId, userName, OnLineUser.onLineUserList);

                //Clients.Caller.onConnected(user.UserID, connectionId, userName, OnLineUser.onLineUserList);
                //Clients.AllExcept(connectionId).newUserConnected(user.UserID, userName, OnLineUser.onLineUserList);

                _context.SaveChanges();
            }

            await base.OnConnectedAsync();
        }
Ejemplo n.º 9
0
        protected override void OnBefore(IInvocation invocation)
        {
            _httpContextAccessor = ServiceTool.ServiceProvider.GetService <IHttpContextAccessor>();

            _loggedInUsers = ServiceTool.ServiceProvider.GetService <LoggedInUsers>();

            var user = _httpContextAccessor.HttpContext.User;

            if (!user.Identity.IsAuthenticated)
            {
                throw new AuthenticationException(AspectMessage.AuthenticationError);
            }

            var personnelId = user.GetPersonnelId();

            if (personnelId == 0)
            {
                throw new AuthenticationException(AspectMessage.AuthenticationError);
            }

            var userInfo = _loggedInUsers.UserInfo.FirstOrDefault(x => x.PersonnelId == personnelId);

            if (userInfo == null)
            {
                throw new SecurityException(AspectMessage.AccessDenied);
            }

            var isAdmin = userInfo.PersonnelType == PersonnelType.Admin;

            if (isAdmin)
            {
                return;
            }

            if (userInfo.PersonnelType != PersonnelType.Personnel)
            {
                throw new SecurityException(AspectMessage.AccessDenied);
            }

            var actions = new[] { "GetAll", "Get", "Insert", "InsertRange", "Update", "Delete", "DeleteRange" };

            var action = invocation.Method.Name.Replace("Async", "");

            if (action.Left(8) == "GetAllBy")
            {
                action = "GetAll";
            }
            if (action.Right(6) == "Change" || action == "SaveRange")
            {
                action = "Update";
            }

            if (_ruleType == RuleType.Null && !actions.Contains(action))
            {
                return;
            }

            if (_module == ApplicationModule.Null)
            {
                var serviceName = invocation.TargetType?.Name ?? "";
                serviceName = serviceName.Left(serviceName.Length - "Service".Length);

                try
                {
                    _module = serviceName.ToEnum <ApplicationModule>();
                }
                catch (Exception e)
                {
                    throw new Exception($"{AspectMessage.ApplicationModuleNotDefined} {e.Message}");
                }
            }

            if (_ruleType == RuleType.Null)
            {
                if (action.Contains("Get"))
                {
                    _ruleType = RuleType.View;
                }
                else if (action.Contains("Insert") || action.Contains("InsertRange"))
                {
                    _ruleType = RuleType.Insert;
                }
                else if (action.Contains("Update"))
                {
                    _ruleType = RuleType.Update;
                }
                else if (action.Contains("Delete") || action.Contains("DeleteRange"))
                {
                    _ruleType = RuleType.Delete;
                }
            }


            var rules = userInfo.Rules.FirstOrDefault(x => x.ApplicationModule == _module);

            var isAuthorized = rules != null && _ruleType switch
            {
                RuleType.View => rules.View,
                RuleType.Insert => rules.Insert,
                RuleType.Update => rules.Update,
                RuleType.Delete => rules.Delete,
                _ => false
            };

            if (isAuthorized)
            {
                return;
            }
            throw new SecurityException(AspectMessage.AccessDenied);
        }
    }
Ejemplo n.º 10
0
 public ChatHub(LoggedInUsers loggedInUsers, IDataAccessRepository <Account> dalAccount)
 {
     _loggedInUsers = loggedInUsers;
     _dalAccount    = dalAccount;
 }