public async Task <ActionResult> Register(RegisterEmpresaViewModel model)
        {
            if (ModelState.IsValid)
            {
                TallerIVDbContext            db = new TallerIVDbContext();
                BaseService <UsuarioEmpresa> usuariosService = new BaseService <UsuarioEmpresa>(db);
                var user   = new UsuarioEmpresa(model.Cuit, model.RazonSocial, DateTime.Now, model.Email, model.Email);
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    UserManager.AddToRole(user.Id, "Empresa");
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string 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 <a href=\"" + callbackUrl + "\">here</a>");

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

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 2
0
        public async Task <ActionResult> RegisterEmpresa(RegisterEmpresaViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName     = model.email,
                    Email        = model.email,
                    ativo        = true,
                    dataCadastro = DateTime.Now
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    NetGoogleGeocoding geoCoder = new NetGoogleGeocoding();

                    string endereco         = model.endereco + ", " + model.numero;
                    string enderecoConsulta = endereco + " - " + model.cidade + " - " + model.estado;

                    Empresas e = new Empresas();
                    e.empresaId   = user.Id;
                    e.cnpj        = model.cnpj;
                    e.razaoSocial = model.razaoSocial;
                    e.email       = model.email;
                    e.telefone    = model.telefone;
                    e.fax         = model.fax;
                    e.endereco    = enderecoConsulta;

                    try
                    {
                        var response = geoCoder.GoogleGeocode(enderecoConsulta).GeoCodes[0];
                        if (response != null)
                        {
                            e.enderecoCoordenada = geoCoder.ConvertLatLonToDbGeometry(response.Longitude, response.Latitude);
                        }
                        else
                        {
                            e.enderecoCoordenada = null;
                        }
                    }
                    catch (Exception)
                    {
                        e.enderecoCoordenada = null;
                    }

                    Roles r = db.Roles.Find("3");
                    if (r == null)
                    {
                        r      = new Roles();
                        r.Id   = "3";
                        r.Name = "Empresa";
                        db.Roles.Add(r);
                    }

                    UsuarioRole ur = new UsuarioRole();
                    ur.UserId = user.Id;
                    ur.RoleId = r.Id;

                    db.UsuarioRole.Add(ur);
                    db.Empresas.Add(e);
                    db.SaveChanges();

                    LoginViewModel lvm = new LoginViewModel()
                    {
                        Email      = model.email,
                        Password   = model.Password,
                        RememberMe = false
                    };

                    return(await Login(lvm, null));
                }
                AddErrors(result);
            }

            ViewBag.retorno = @Url.Action("Login", "Account");

            return(View(model));
        }