コード例 #1
0
        public void Login(User user)
        {
            var claims = new List<Claim>(new[]
            {
                new Claim(ClaimTypes.NameIdentifier, user.Username),
                new Claim(ClaimTypes.Name, user.Name)
            });

            if (!string.IsNullOrWhiteSpace(user.Email))
            {
                claims.Add(new Claim(ClaimTypes.Email, user.Email));
            }

            claims.AddRange(user.Roles.Select(role => new Claim(ClaimTypes.Role, role.Name)));

            var identity = new ClaimsIdentity(claims, "Forms");
            var principal = new ClaimsPrincipal(identity);

            var sessionToken = new SessionSecurityToken(principal);
            var authenticationModule = FederatedAuthentication.SessionAuthenticationModule;

            // Persist the authentication cookie.
            authenticationModule.WriteSessionTokenToCookie(sessionToken);

            // Set the current user for the current request
            Thread.CurrentPrincipal = principal;
        }
コード例 #2
0
        public void AddUserToRoles(User user, params string[] rolesToAddTo)
        {
            var session = sessionCallback();

            foreach (var roleToAddTo in rolesToAddTo)
            {
                var role = session.QueryOver<Role>().Where(r => r.Name == roleToAddTo).SingleOrDefault();
                role.Users.Add(user);
                user.Roles.Add(role);
                session.SaveOrUpdate(role);
            }
        }
コード例 #3
0
        public User CreateAccount(string name, string email, string username, string password)
        {
            var user = new User
                           {
                               Name = name,
                               Email = email,
                               Password = HashPassword(password),
                               Username = username
                           };

            DependencyResolver.Current.GetService<ISession>().Save(user);
            return user;
        }
コード例 #4
0
        public void AddUserToRoles(User user, params Claim[] rolesToAdd)
        {
            var session = sessionCallback();

            foreach (var roleToAddTo in rolesToAdd)
            {
                Claim roleClaim = roleToAddTo;
                var role = session.QueryOver<Model.Authentication.Role>().Where(r => r.Name == roleClaim.Value).SingleOrDefault();
                role.Users.Add(user);
                user.Roles.Add(role);
                session.SaveOrUpdate(role);
            }
        }