示例#1
0
        public Usuario AddUsuario(RegisterViewModel usuario)
        {
            using (TimeSContext db = new TimeSContext())
            {
                byte[] img;
                try
                {
                    img = ConverterStringWebImageToBinaryImage(usuario.Foto);
                }
                catch (Exception ex)
                {
                    Debug.Write(ex.Message);
                    img = getFileBytes("\\Media\\img\\pic\\Storm-Trooper.jpg");
                }
                //byte[] img = getFileBytes("\\Media\\img\\pic\\Storm-Trooper.jpg");
                var store = new UserStore<Usuario>(db);
                var manager = new UserManager<Usuario>(store);
                var novousuario = new Usuario() { Nome = usuario.Nome, UserName = usuario.Email, Email = usuario.Email, Foto = img, Ativo=true };
                try
                {
                    manager.Create(novousuario, usuario.Password);
                    manager.AddToRole(novousuario.Id, "Comum");
                    return novousuario;
                }
                catch (Exception ex)
                {
                    Debug.Write(ex.Message);
                    return null;
                }

            }
        }
示例#2
0
 public Usuario EditarUsuario(Usuario usuario)
 {
     using (TimeSContext db = new TimeSContext())
     {
         try
         {
             db.Entry(usuario).State = EntityState.Modified;
             db.SaveChanges();
             return usuario;
         }
         catch (Exception ex)
         {
             Debug.Write(ex.Message);
             return null;
         }
     }
 }
示例#3
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new Usuario { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
示例#4
0
        public async Task<ActionResult> Register(RegisterViewModel model, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                var user = new Usuario() { UserName = model.Email, Email = model.Email, Nome = model.Nome };
                if (image != null)
                {
                    user.ImageMimeType = image.ContentType;
                    user.Foto = new byte[image.ContentLength];
                    image.InputStream.Read(user.Foto, 0, image.ContentLength);
                }
                var result = await UserManager.CreateAsync(user, model.Password);
                UserManager.AddToRole(user.Id, "Comum");
                if (result.Succeeded)
                {
                    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 RedirectToLocal("/");
                }
                AddErrors(result);
            }

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