private GenericIdentity CreateIdentity(string username, User modelUser)
 {
     var identity = new GenericIdentity(username, BasicScheme);
     identity.AddClaim(new Claim(ClaimTypes.Sid, modelUser.UserId.ToString()));
     identity.AddClaim(new Claim(ClaimTypes.GivenName, modelUser.Firstname));
     identity.AddClaim(new Claim(ClaimTypes.Surname, modelUser.Lastname));
     identity.AddClaim(new Claim(ClaimTypes.Email, modelUser.Email));
     return identity;
 }
        public MockPrincipal(string userId)
        {
            if (default(string) == userId)
                userId = Guid.NewGuid().ToString();
            var identity = new GenericIdentity(userId);

            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userId));
            identity.AddClaim(new Claim(ClaimTypes.Name, userId));
            this.Identity = identity;
        }
        private IPrincipal GetPrincipal(User user)
        {
            var identity = new GenericIdentity(user.Username, Constants.SchemeTypes.Basic);

            identity.AddClaim(new Claim(ClaimTypes.GivenName, user.Firstname));
            identity.AddClaim(new Claim(ClaimTypes.Surname, user.Lastname));

            var username = user.Username.ToLowerInvariant();
            switch(username)
            {
                case "bhogg":
                    identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.Manager));
                    identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.SeniorWorker));
                    identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.JuniorWorker));
                    break;
                case "jbob":
                    identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.SeniorWorker));
                    identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.JuniorWorker));
                    break;
                case "jdoe":
                    identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.JuniorWorker));
                    break;
                default:
                    return null;
            }

            return new ClaimsPrincipal(identity);

        }
        /// <summary>
        /// Builds the Principal
        /// </summary>
        public virtual IPrincipal GetPrincipal(User user)
        {
            var identity = new GenericIdentity(user.UserName, Constants.SchemeTypes.Basic);

            identity.AddClaim(new Claim(ClaimTypes.GivenName, user.FirstName));
            identity.AddClaim(new Claim(ClaimTypes.Surname, user.LastName));
            identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.User));

            var username = user.UserName.ToLowerInvariant();
            if (username == "bhogg")
            {
                identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.Admin));
            }

            return new ClaimsPrincipal(identity);
        }
        private void SetupIdentity(string osmUserId = "42")
        {
            var identity = new GenericIdentity(osmUserId);
            identity.AddClaim(new Claim(ClaimTypes.Name, osmUserId));

            Thread.CurrentPrincipal = new GenericPrincipal(identity, new string[0]);
        }
        private GenericIdentity CreateIdentity(string username, User modelUser)
        {
            var identity = new GenericIdentity(username, BasicScheme);
            identity.AddClaim(new Claim(ClaimTypes.Sid, modelUser.Id.ToString()));

            return identity;
        }
        /// <summary>
        /// This method will create an IIdentity & IPrincipal objects
        /// and sets it to HttpContext.Current.User property, so that the IPrincipal
        /// object is available across HttpContext.
        /// </summary>
        /// <param name="user">User data</param>
        /// <param name="sessionToken">Session Token key</param>
        /// /// <param name="roles">Roles</param>
        private static void SetPrincipal(User user, string sessionToken,string[] roles)
        {
            var identity = new GenericIdentity(user.UserName, SessionSchema);

            identity.AddClaim(new Claim(ClaimTypes.Sid, user.Id.ToString()));
            identity.AddClaim(new Claim(ClaimTypes.PrimaryGroupSid, user.Payor.Id.ToString()));
            identity.AddClaim(new Claim(CustomClaimType.PayorName.ToString(), user.Payor.Name));
            identity.AddClaim(new Claim(ClaimTypes.PrimarySid, sessionToken));

            
            var principal = new GenericPrincipal(identity, roles);

            Thread.CurrentPrincipal = principal;

            if (HttpContext.Current != null)
            {
                HttpContext.Current.User = principal;
            }
        }
        public void GivenClaimsPrincipal_AndIdentityHasNameIdentifierClaim_WhenGetUserKey_ThenGetNameIdentifierClaimValue()
        {
            string expected = "this is the user key";
            GenericIdentity identity = new GenericIdentity("whatever");
            GenericPrincipal principal = new GenericPrincipal(identity, null);
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, expected));

            string actual = EducationSecurityPrincipal.FindUserKey(principal);

            Assert.AreEqual(expected, actual);
        }
        private void MockIdentity()
        {
            // http://forums.asp.net/t/2028867.aspx?UnitTest+How+to+Mock+User+Identity+GetUserId+
            var context = new Mock<HttpContextBase>();
            var request = new Mock<HttpRequestBase>();

            var identity = new GenericIdentity("*****@*****.**");
            identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "1"));
            var principal = new GenericPrincipal(identity, new[] { "user" });
            context.Setup(s => s.User).Returns(principal);

            this.controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), this.controller);
        }
        public async Task ShouldRedirectWhenDeviceIsCreated()
        {
            Device device = new Device { Name = "Raspberry Pi" };
            var mock = new Mock<IDeviceRepository>();
            mock.Setup(m => m.GetDeviceByNameAsync(device.Name))
                .ReturnsAsync(null);

            var context = new Mock<HttpContextBase>();
            var identity = new GenericIdentity("*****@*****.**");
            identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "1"));
            var principal = new GenericPrincipal(identity, new[] { "user" });
            context.Setup(s => s.User).Returns(principal);

            IDeviceRepository deviceRepository = mock.Object;
            IMeasurementUnitRepository measurementUnitRepository = Mock.Of<IMeasurementUnitRepository>();
            DeviceController controller = new DeviceController(deviceRepository, measurementUnitRepository);
            controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);

            controller.WithCallTo(c => c.Create(device))
                      .ShouldRedirectTo(c => c.Details(1));
        }
        internal void SetCurrentIdentity(User user, Role role)
        {
            var identity = new GenericIdentity(user.Username);
            identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", user.Id.ToString()));

            Thread.CurrentPrincipal = new GenericPrincipal(identity, new[] { role.Name });

            UserHelper.Setup(x => x.GetLoggedUser(It.IsAny<HttpRequestMessage>())).Returns(user);
            UserHelper.Setup(x => x.GetLoggedUserRole(It.IsAny<HttpRequestMessage>())).Returns(new Role { Id = role.Id, Name = role.Name });

            UserController = new UserController(UserHelper.Object);
        }
        private void LoginMock(bool isAuthenticated)
        {
            var identity = new GenericIdentity(this.user.UserName);
            identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", this.user.Id));
            var principal = new GenericPrincipal(identity, new[] { "user" });

            var controllerContext = new Mock<ControllerContext>();

            var httpContext = new Mock<HttpContextBase>();
            var request = new Mock<HttpRequestBase>();
            controllerContext.SetupGet(x => x.HttpContext).Returns(httpContext.Object);
            controllerContext.SetupGet(x => x.HttpContext.Request).Returns(request.Object);
            controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal);
            controllerContext.SetupGet(x => x.HttpContext.Request.IsAuthenticated)
                .Returns(isAuthenticated);

            this.meController.ControllerContext = controllerContext.Object;
        }
        private GenericIdentity CreateIdentity(MembershipProviderUser user)
        {
            var identity = new GenericIdentity(user.Username, BasicScheme);
            identity.AddClaim(new Claim(ClaimTypes.Sid, user.UserId));
            identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));

            if (GetAdditionalClaims != null)
            {
                try
                {
                    var claims = GetAdditionalClaims(user);
                    identity.AddClaims(claims);
                }
                catch (Exception exception)
                {
                    const string msg = "Error getting additional claims from caller";
                    Debug.WriteLine(msg + ": " + exception);
                    throw new Exception(msg, exception);
                }
            }

            return identity;
        }