public void ApplicationUserCollectionsAreNotNulls()
        {
            WebUser user = new WebUser();

            // Check all collections (claims, roles) - they must be initialized as empty, but not nulls
            foreach (PropertyInfo objList in typeof(WebUser).GetProperties().Where(pt => pt.PropertyType.IsGenericType))
            {
                if (objList.PropertyType.Name.StartsWith("ICollection"))
                {
                    objList.GetValue(user, null).Should().NotBeNull();
                }
            }
        }
        public void PrepareController()
        {
            this.webUser = new WebUser
            {
                Id = RandomData.GetInteger(10000, 99999),
                UserName = RandomData.GetEmailAddress(),
                BirthDate = RandomData.GetDateTimeInPast(),
                EmailConfirmed = true,
                FirstName = RandomData.GetStringPersonFirstName(),
                LastName = RandomData.GetStringPersonLastName(),
                SecurityStamp = Guid.NewGuid().ToString(),
                Mobile = RandomData.GetStringNumber(8),
                PersonCode = RandomData.GetPersonalIdentificationCodeFinnish(),
                IsStronglyAuthenticated = false,
                CustomerId = RandomData.GetInteger(10000000, 39999999)
            };

            this.userStoreMock = new Mock<IUserStore<ApplicationUser, int>>();
            this.authManagerMock = new Mock<IAuthenticationManager>();

            this.userManagerMock = new Mock<IApplicationUserManager>();
            this.userManagerMock.Setup(mock => mock.FindByEmailAsyncWrap(It.IsAny<string>())).Returns(Task.FromResult(this.webUser));
            this.userManagerMock.Setup(mock => mock.FindByNameAsyncWrap(It.IsAny<string>())).Returns(Task.FromResult(this.webUser));
            this.userManagerMock.Setup(mock => mock.FindByIdAsyncWrap(It.IsAny<int>())).Returns(Task.FromResult(this.webUser)); 
            this.userManagerMock.Setup(mock => mock.IsEmailConfirmedAsyncWrap(It.IsAny<int>())).Returns(Task.FromResult(true));

            this.signInManangerMock = new Mock<IApplicationSignInManager>();
            this.signInManangerMock.Setup(
                mock => mock.PasswordSignInAsyncWrap(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>()))
                .Returns(Task.FromResult(SignInStatus.Success));
            
            this.controllerContext = HttpMocks.GetControllerContextMock(HttpMocks.GetHttpContextMock()).Object;
            var routes = new RouteCollection();
            routes.Add(
                "Default",
                new Route(
                    "{controller}/{action}/{id}",
                    new RouteValueDictionary(new { controller = "Home", action = "Index", area = string.Empty, id = UrlParameter.Optional }),
                    new HyphenatedRouteHandler()));
            this.urlHelper = HttpMocks.GetUrlHelper(routes);
            this.SetAcountController(this.userManagerMock.Object, this.signInManangerMock.Object, this.authManagerMock.Object);
        }
        public virtual async Task<ActionResult> StrongUserCreate(UserProfileModel model)
        {
            if (model == null)
            {
                // TODO: Add master data translations for those
                this.WebMessages.AddErrorMessage(this.T("User profile"), this.T("User profile data was not present in request"));
                return this.RedirectBack();
            }

            if (!this.ModelState.IsValid)
            {
                return this.View(MVC.Account.Views.UserProfileStrongEdit, model);
            }

            var existingUser = await this.UserManager.FindByEmailAsyncWrap(model.Email);
            if (existingUser != null)
            {
                // TODO: Add master data translations for those
                this.WebMessages.AddInfoMessage(this.T("User account"), this.T("User with given e-mail address already exists. Please, signIn with e-mail address and password and then upgrade account."));
                return this.RedirectToAction(MVC.Account.Login());
            }

            var result = await this.UserManager.CreateAsyncWrap(model.Email, null);
            if (result.Item1.Succeeded && result.Item2 != 0)
            {
                WebUser user = new WebUser().FromUserProfile(model);
                user.Id = result.Item2;
                user.IsStronglyAuthenticated = true;
                var updateResult = await this.UserManager.UpdateAsyncWrap(result.Item2, user);
                if (updateResult.Succeeded)
                {
                    // Sign in Vetuma user automatically
                    await this.SignInManager.SignInAsyncWrap(user, true, false);

                    // Create all necessary information to send e-mail verification
                    string callbackUrl = await this.SendEmailVerificationToken(user.Id);
#if !PROD
                    // For testing this will show same link in next page, to ease confirmation procedure
                    ViewBag.Link = callbackUrl;
#endif
                    return this.View(MVC.Account.Views.DisplayEmailConfirmation);
                }

                return this.RedirectBack();
            }

            foreach (var errorMessage in result.Item1.Errors)
            {
                this.WebMessages.AddErrorMessage(errorMessage);
            }

            return this.View(MVC.Account.Views.RegistrationForm, model);
        }
        /// <summary>
        ///  See: http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/
        ///  SignIn user via Owin
        ///  ClaimTypes.NameIdentifier is unique claim type
        /// </summary>
        /// <param name="user">The user object, validated.</param>
        private void SignInVetumaUser(WebUser user)
        {
            var claims = new List<Claim>();
            claims.Add(new Claim(ClaimTypes.Name, user.UserName));
            claims.Add(new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()));

            var id = new ClaimsIdentity(claims,
                                        DefaultAuthenticationTypes.ApplicationCookie);

            var ctx = Request.GetOwinContext();
            var authenticationManager = ctx.Authentication;
            authenticationManager.SignIn(id);
        }