public IHttpActionResult PostUsuario(UsuarioSenha usuarioSenha)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var usuario = new Usuario
            {
                Endereco  = usuarioSenha.Endereco,
                Professor = false,
                Estudante = true,
                Sobrenome = usuarioSenha.Sobrenome,
                Telefone  = usuarioSenha.Telefone,
                UserName  = usuarioSenha.UserName
            };


            try
            {
                db.Usuarios.Add(usuarioSenha);
                db.SaveChanges();
                Uteis.CreateUserASP(usuarioSenha.UserName, usuarioSenha.Senha);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
            }

            usuarioSenha.UserId    = usuario.UserId;
            usuarioSenha.Professor = false;
            usuarioSenha.Estudante = true;

            return(this.Ok(usuarioSenha));
        }
Beispiel #2
0
        public ActionResult Create(UsuarioView view)
        {
            if (ModelState.IsValid)
            {
                db.Usuarios.Add(view.Usuario);

                try
                {
                    if (view.Foto != null)
                    {
                        var pic = Uteis.UploadPhoto(view.Foto);
                        if (!string.IsNullOrEmpty(pic))
                        {
                            view.Usuario.Photo = string.Format("~/Content/Fotos/{0}", pic);
                        }
                    }
                    db.SaveChanges();
                    Uteis.CreateUserASP(view.Usuario.UserName);

                    if (view.Usuario.Estudante)
                    {
                        Uteis.AddRoleToUser(view.Usuario.UserName, "Aluno");
                    }

                    if (view.Usuario.Professor)
                    {
                        Uteis.AddRoleToUser(view.Usuario.UserName, "Professor");
                    }

                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                }
            }

            return(View(view));
        }