Exemple #1
0
 public void AddDislike(Page page, User user)
 {
     page.Dislikes.Add(new Dislike
     {
         PageId = page.Id,
         UserId = user.Id
     });
 }
Exemple #2
0
 public void AddLike(Page page, User user)
 {
     page.Likes.Add(new Like
     {
         PageId = page.Id,
         UserId = user.Id
     });
 }
Exemple #3
0
 public void AddComment(Page page, User user, string content)
 {
     page.Comments.Add(new Comment
     {
         AuthorId = user.Id,
         PageId = page.Id,
         Content = content
     });
 }
Exemple #4
0
        public void SeedAdmin(LikeItDbContext context)
        {
            var userManager = new UserManager<User>(new UserStore<User>(context));
            var admin = new User()
            {
                Email = "[email protected]",
                UserName = "******",
                FirstName = "Adi",
                LastName = "Minkov"
            };

            userManager.Create(admin, "123456");
            userManager.AddToRole(admin.Id, GlobalConstants.AdminRole);

            context.SaveChanges();
        }
Exemple #5
0
        public void SeedSinglePage(ILikeItDbContext context, string name, string description, Category category, IList<string> tags, User user, Image image, bool like, string comment = "")
        {
            var page = new Page
            {
                Name = name,
                Category = category,
                Description = description,
                CreatedOn = DateTime.Now,
                User = user,
                Image = image,
            };

            for (int i = 0; i < tags.Count; i++)
            {
                this.AddTag(page, tags[i]);
            }

            if (like)
            {
                this.AddLike(page, page.User);
            }
            else
            {
                this.AddDislike(page, page.User);
            }

            page.Rating = this.GetPageRating(page);

            if (!string.IsNullOrEmpty(comment))
            {
                this.AddComment(page, page.User, comment);
            }

            context.Pages.Add(page);
            context.SaveChanges();
        }
Exemple #6
0
        public void SeedRandomUsers(LikeItDbContext context, int count)
        {
            var userManager = new UserManager<User>(new UserStore<User>(context));

            for (int i = 0; i < count; i++)
            {
                var user = new User
                {
                    Email = string.Format("{0}@{1}.com", this.randomGenerator.RandomString(3, 6), this.randomGenerator.RandomString(3, 6)),
                    UserName = this.randomGenerator.RandomString(6, 16),
                    FirstName = this.randomGenerator.RandomString(6, 16),
                    LastName = this.randomGenerator.RandomString(6, 16)
                };

                userManager.Create(user, "123456");
            }

            context.SaveChanges();
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                // TODO: Improve
                var existingUserName = await UserManager.FindByNameAsync(model.Username);
                var existingEmail = await UserManager.FindByEmailAsync(model.Email);

                if (existingUserName != null)
                {
                    ModelState.AddModelError("", "Username already exists.");
                }
                else if (existingEmail != null)
                {
                    ModelState.AddModelError("", "Email already exists.");
                }
                else
                {
                    var user = new User()
                    {
                        UserName = model.Username,
                        Email = model.Email,
                        FirstName = model.FirstName,
                        LastName = model.LastName
                    };
                    IdentityResult result = await UserManager.CreateAsync(user, model.Password);

                    var userRole = GlobalConstants.UserRole;
                    this.UserManager.AddToRole(user.Id, userRole);

                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: 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");
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return this.View(model);
        }
 private async Task SignInAsync(User user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager));
 }
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return this.View("ExternalLoginFailure");
                }
                var user = new User() { UserName = model.Email, Email = model.Email };
                IdentityResult result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await this.UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: 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);
                        // SendEmail(user.Email, callbackUrl, "Confirm your account", "Please confirm your account by clicking this link");
                        
                        return RedirectToLocal(returnUrl);
                    }
                }

                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return this.View(model);
        }
 protected override IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state)
 {
     this.CurrentUser = this.data.Users.All().Where(u => u.UserName == requestContext.HttpContext.User.Identity.Name).FirstOrDefault();
     return base.BeginExecute(requestContext, callback, state);
 }