private static void MockUser() { using (var uow = Ioc.Container.Get <ILaPazUow>()) { var defaultUserId = Guid.Parse("4FB4CAF7-9FD7-4A39-BF85-B60F14C2E7AB"); var defaultSucursalId = 2; //Validate credentials through the authentication service Operador user = uow.Operadores.Obtener(o => o.Id == defaultUserId, o => o.Roles, o => o.Personal, o => o.Personal.Provincia, o => o.Personal.Localidad, o => o.OperadorSucursals.Select(op => op.Sucursal)); Sucursal sucursal = uow.Sucursales.Obtener(s => s.Id == defaultSucursalId); //Get the current principal object LaPazPrincipal laPazPrincipal = Thread.CurrentPrincipal as LaPazPrincipal; if (laPazPrincipal == null) { throw new ArgumentException("The application's default thread principal must be set to a CustomPrincipal object on startup."); } //Authenticate the user laPazPrincipal.Identity = new LaPazIdentity(user, sucursal); } }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); #if (!DEBUG) Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += ApplicationOnThreadException; AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; #endif AutoMapperConfig.Execute(); MetadataTypesRegister.InstallForThisAssembly(); AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; using (var kernel = new StandardKernel()) { //Configurar bindings DIConfig.Configure(kernel); //Set global container. Ioc.Container = new NinjectIocContainer(kernel); //Config log4net log4net.Config.DOMConfigurator.Configure(); MessageBoxDisplayService = Ioc.Container.Get <IMessageBoxDisplayService>(); //Create a custom principal with an anonymous identity at startup var laPazPrincipal = new LaPazPrincipal(); AppDomain.CurrentDomain.SetThreadPrincipal(laPazPrincipal); #if (MOCK_SECURITY) MockUser(); #else using (var login = kernel.Get <FrmLogin>()) { var result = login.ShowDialog(); if (result == DialogResult.Cancel) { Application.Exit(); return; } } #endif RunAfterLoginTasks(); var mainForm = kernel.Get <FrmPrincipal>(); Application.Run(mainForm); } }
private void BtnIngresar_Click(object sender, EventArgs e) { var clearTextPassword = TxtPassword.Text; var username = CbxUsuario.Usuario; this.FormErrorProvider.Clear(); if (string.IsNullOrEmpty(username)) { this.FormErrorProvider.SetError(CbxUsuario, "Debe ingresar un usuario"); this.DialogResult = DialogResult.None; return; } if (string.IsNullOrEmpty(clearTextPassword)) { this.FormErrorProvider.SetError(TxtPassword, "Debe ingresar una clave"); this.DialogResult = DialogResult.None; return; } if (UcSucursales.Sucursal == null) { this.FormErrorProvider.SetError(UcSucursales, "Debe seleccionar una sucursal"); this.DialogResult = DialogResult.None; return; } try { //Validate credentials through the authentication service Operador user = _authenticationService.AuthenticateUser(username, clearTextPassword); Sucursal sucursal = UcSucursales.Sucursal; //Get the current principal object LaPazPrincipal laPazPrincipal = Thread.CurrentPrincipal as LaPazPrincipal; if (laPazPrincipal == null) { throw new ArgumentException("The application's default thread principal must be set to a CustomPrincipal object on startup."); } var sucursalValida = user.OperadorSucursals.Any(s => s.SucursalId == sucursal.Id); if (!sucursalValida) { this.FormErrorProvider.SetError(UcSucursales, "No esta habilitado para ingresar con esta sucursal"); this.DialogResult = DialogResult.None; return; } if (RolesAceptados != null && RolesAceptados.Any()) { if (!(user.Roles.Select(r => r.Description).Any(r => RolesAceptados.Any(ra => ra == r)))) { _messageBoxDisplayService.ShowError(Resources.ErrorMessageRolesRequeridos); this.DialogResult = DialogResult.None; return; } } if (CambiarIdentity) { //Authenticate the user laPazPrincipal.Identity = new LaPazIdentity(user, sucursal); } UltimoOperadorIngresado = user; } catch (UnauthorizedAccessException) { _messageBoxDisplayService.ShowError("La credenciales ingresadas no son correctas"); this.DialogResult = DialogResult.None; } }