Beispiel #1
0
        public async Task <ActionResult> Register(MultipleClassRegistrar model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.objRegistrar.Email, Email = model.objRegistrar.Email
                };
                var result = await UserManager.CreateAsync(user, model.objRegistrar.Password);

                if (result.Succeeded)
                {
                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");

                    ViewBag.Link = callbackUrl;
                    //return View("DisplayEmail");
                }
                AddErrors(result);

                string idcuenta = user.Id;

                aspnetroles asprol = db.aspnetroles.FirstOrDefault(r => r.Name == "Administrador");

                var rol = new aspnetuserroles
                {
                    UserId = idcuenta,
                    RoleId = asprol.Id
                };

                db.aspnetuserroles.Add(rol);
                db.SaveChanges();

                var emp = new empresas
                {
                    Emp_Nom = model.objEmpresas.Emp_Nom
                };
                db.empresas.Add(emp);
                db.SaveChanges();

                int empresa_id = emp.Emp_Id;

                var log = new login
                {
                    Emp_Id = empresa_id,
                    Id     = idcuenta
                };

                db.login.Add(log);
                db.SaveChanges();

                return(RedirectToAction("Index", "Home"));
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #2
0
        public void ConfigureAuth(IAppBuilder app)
        {
            // Fix DbContext
            var applicationUserManager =
                new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext()));

            /*
             *
             * if (filterContext.RouteData.Values.ContainsValue("Cancel"))
             * {
             *  filterContext.Result = new RedirectResult("~/Home/Index");
             *  //Trace.WriteLine(" Redirecting from Simple filter to /Home/Index");
             * }
             *
             *
             * app.Use(async (context, next) =>
             * {
             *  // Do work that doesn't write to the Response.
             *  await next.Invoke();
             *  // Do logging or other work that doesn't write to the Response.
             * });
             * /*
             * app.Run(async context =>
             * {
             *  await context.Response.WriteAsync("Hello from 2nd delegate.");
             * });
             *
             * app.Run(async (context, next) =>
             * {
             *
             *  CommonManager commonManager = new CommonManager();
             *  var usuarioAutenticado = commonManager.GetUsuarioAutenticado();
             *  if(usuarioAutenticado.Tipo == "PROVEEDOR"|| usuarioAutenticado.Tipo == "PROVEEDOR")
             *  {
             *      context.Response.ContentType = "text/plain";
             *      await context.Response.WriteAsync("Hello ASP.NET 5!");
             *
             *  }
             * });
             *
             */


            // SuperUsuario
            var usuarioManager = new UsuarioManager();


            //Roles
            var db = new EntitiesDap();
            //AFI
            var rolesNazan = GetRolesNazan();

            foreach (var role in rolesNazan)
            {
                if (db.aspnetroles
                    .FirstOrDefault(r => r.Name == role.Key) != null)
                {
                    continue;
                }

                var aspnetrole = new aspnetroles()
                {
                    Id          = role.Key,
                    Name        = role.Key,
                    Description = role.Value,
                    Tipo        = "DAP"
                };

                db.aspnetroles.Add(aspnetrole);
                db.SaveChanges();
            }
            //AFI
            var rolesMercaderia = GetRolesMercaderia();

            foreach (var role in rolesMercaderia)
            {
                if (db.aspnetroles
                    .FirstOrDefault(r => r.Name == role.Key) != null)
                {
                    continue;
                }

                var aspnetrole = new aspnetroles()
                {
                    Id          = role.Key,
                    Name        = role.Key,
                    Description = role.Value,
                    Tipo        = "MERCADERIA"
                };

                db.aspnetroles.Add(aspnetrole);
                db.SaveChanges();
            }

            //AFI
            var rolesServicio = GetRolesServicio();

            foreach (var role in rolesServicio)
            {
                if (db.aspnetroles
                    .FirstOrDefault(r => r.Name == role.Key) != null)
                {
                    continue;
                }

                var aspnetrole = new aspnetroles()
                {
                    Id          = role.Key,
                    Name        = role.Key,
                    Description = role.Value,
                    Tipo        = "SERVICIO"
                };

                db.aspnetroles.Add(aspnetrole);
                db.SaveChanges();
            }


            if (applicationUserManager.FindByName("superusuario") == null)
            {
                usuarioManager.CrearNazan(

                    "superusuario",
                    "superusuario",
                    "superusuario",
                    "superusuario",
                    null,
                    null,
                    true,
                    PerfilManager.MaestroNazan.Id,
                    "123456");
            }

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath          = new PathString("/Account/Login"),
                ExpireTimeSpan     = TimeSpan.FromMinutes(30)
            });

            CargarConfiguraciones();
        }
        public async Task <ActionResult> CuentaPersonas(MultiplesClases model, string subemp_id, string per_rut, string car_nom)
        {
            if (ModelState.IsValid)
            {
                string empresa = HttpContext.Session["Empresa"].ToString();

                var user = new ApplicationUser {
                    UserName = model.ObjRegistrar.Email, Email = model.ObjRegistrar.Email
                };
                var result = await UserManager.CreateAsync(user, model.ObjRegistrar.Password);

                if (result.Succeeded)
                {
                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                }
                AddErrors(result);

                string   idcuenta = user.Id;
                personas personas = db.personas.FirstOrDefault(p => p.Per_Rut == per_rut);
                //var cargos = db.cargos.FirstOrDefault(c => c.Car_Id == personas.Car_Id);



                aspnetroles asprol = db.aspnetroles.FirstOrDefault(r => r.Name == car_nom);

                aspnetuserroles rol = new aspnetuserroles
                {
                    UserId = idcuenta,
                    RoleId = asprol.Id
                };


                db.aspnetuserroles.Add(rol);
                db.SaveChanges();



                var empresas = db.empresas.FirstOrDefault(e => e.Emp_Nom == empresa);

                int empresa_id = empresas.Emp_Id;

                login log = new login
                {
                    Emp_Id  = empresa_id,
                    Id      = idcuenta,
                    Per_Rut = per_rut
                };

                db.login.Add(log);
                db.SaveChanges();

                //contratos contratos = db.contratos.FirstOrDefault(c=>c.Per_Rut==per_rut);

                //int subemp_id=contratos.Sub_Id;


                return(RedirectToAction("Index", "Contratos", new { subemp_id }));
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }