Beispiel #1
0
        public async Task <ActionResult> SetPassword(SetPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);

                if (result.Succeeded)
                {
                    var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

                    // spremi promjene također i u bazu podataka
                    context = new Guess_WhereEntities1();

                    RegisteredUser u = context.RegisteredUser.First(x => x.email == user.Email);
                    u.password = model.NewPassword.GetHashCode().ToString();

                    context.SaveChanges();

                    if (user != null)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);
                    }
                    return(RedirectToAction("Index", new { Message = ManageMessageId.SetPasswordSuccess }));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #2
0
        //
        // GET: /Manage/Index
        public async Task <ActionResult> Index(ManageMessageId?message)
        {
            ViewBag.StatusMessage =
                message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
                : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
                : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set."
                : message == ManageMessageId.Error ? "An error has occurred."
                : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added."
                : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed."
                : "";

            context = new Guess_WhereEntities1();

            var userId        = User.Identity.GetUserId();
            var userUserName  = User.Identity.GetUserName();
            var identifikator = context.User.Where(x => x.username == userUserName).First();

            var model = new IndexViewModel
            {
                Avatar = context.RegisteredUser.Where(x => x.IDuser == identifikator.IDuser).SingleOrDefault().avatar,
                //Avatar = context.RegisteredUser.Find(userId).avatar, //how to get the avatar of the current user?
                HasPassword       = HasPassword(),
                PhoneNumber       = await UserManager.GetPhoneNumberAsync(userId),
                TwoFactor         = await UserManager.GetTwoFactorEnabledAsync(userId),
                Logins            = await UserManager.GetLoginsAsync(userId),
                BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId)
            };

            ViewBag.username = userUserName;
            ViewBag.idUser   = identifikator.IDuser;

            return(View(model));
        }
        public async Task<ActionResult> Register(HttpPostedFileBase upload, RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (upload != null && upload.ContentLength > 0)
                {
                    using (var reader = new System.IO.BinaryReader(upload.InputStream))
                    {
                        model.Avatar = reader.ReadBytes(upload.ContentLength);
                    }
                }
                var user = new ApplicationUser { UserName = model.Username, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    context = new Guess_WhereEntities1();
                    User u = new User();
                    bool exist = false;

                    foreach (var korisnik in context.User)
                    {
                        if (korisnik.username == model.Username)
                        {
                            exist = true;
                            u.IDuser = korisnik.IDuser;
                            break;
                        } 
                    }

                    if (!exist)
                    {
                        context.User.Add(new User
                        {
                            username = model.Username
                        });

                        context.RegisteredUser.Add(new RegisteredUser
                        {
                            email = model.Email,
                            password = model.Password.GetHashCode().ToString(),
                            avatar = model.Avatar
                        });
                    }
                    else
                    {
                        context.RegisteredUser.Add(new RegisteredUser
                        {
                            email = model.Email,
                            password = model.Password.GetHashCode().ToString(),
                            avatar = model.Avatar,
                            IDuser = u.IDuser
                        });
                    }                   

                    context.SaveChanges();

                    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);
        }