public void AuthorizeAreasFilterContextResultShouldBeNullIfUserDontHaveRoleButOwnesTheArea()
        {
            var principalFake = new Mock<IPrincipal>();
            principalFake.Setup(x => x.IsInRole("admin")).Returns(false);
            principalFake.Setup(x => x.Identity).Returns(new IdentityFake());

            var httpContextFake = new Mock<HttpContextBase>();
            httpContextFake.Setup(x => x.User).Returns(principalFake.Object);

            //Setup fake RouteData
            var routeDataFake = new RouteData();
            routeDataFake.Values.Add("id", "1");

            var requestContextFake = new Mock<RequestContext>();
            requestContextFake.Setup(x => x.RouteData).Returns(routeDataFake);

            var filterContextFake = new Mock<ActionExecutingContext>();
            filterContextFake.Setup(x => x.HttpContext).Returns(httpContextFake.Object);

            var filterContext = filterContextFake.Object;
            filterContext.RequestContext = requestContextFake.Object;

            var userWithId = new User
            {
                Id = "1",
                Areas = new List<Area> { new Area { Id = 1 } }
            };

            var usersServicesFake = new Mock<IUsersServices>();
            usersServicesFake.Setup(x => x.GetById(It.IsAny<string>())).Returns(userWithId);
            var userServices = usersServicesFake.Object;

            var roles = new string[] { "admin" };
            var authorizeAreaFilter = new AuthorizeArea();
            authorizeAreaFilter.RolesRequired = roles;
            authorizeAreaFilter.UsersServices = userServices;

            authorizeAreaFilter.OnActionExecuting(filterContext);

            Assert.AreEqual(null, filterContext.Result);
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User
                {
                    UserName = model.UserName,
                    Email = model.Email,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    PhoneNumber = model.PhoneNumber,
                    RegisterDate = DateTime.UtcNow
                };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        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 User { 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 ActionResult Login(User user)
        {
            var model = from r in db.Users
                        where r.Name == user.Name && r.Password == user.Password
                        select r;
            if (model != null)
            {
                foreach (var user1 in model)
                {
                    return RedirectToAction("Admin");
                }
            }

            return RedirectToAction("Login", "Admin");
        }
示例#5
0
 public ActionResult Login()
 {
     User user = new User();
     return View(user);
 }