예제 #1
0
        public override void Initialize(string name, NameValueCollection config)
        {
            this.Log("name " + name, MethodBase.GetCurrentMethod().Name);

            this.userNameAD = config["usernameAD"];

            if (this.userNameAD == null)
            {
                throw new ProviderException("Missing required property 'usernameAD'.");
            }

            config.Remove("usernameAD");

            this.passwordAD = config["passwordAD"];


            if (this.passwordAD == null)
            {
                throw new ProviderException("Missing required property 'passwordAD'.");
            }

            config.Remove("passwordAD");

            this.connectionString = config["connectionStringName"];

            if (this.connectionString == null)
            {
                throw new ProviderException("Missing required property 'connectionStringName'.");
            }

            // test for connection
            try
            {
                using (SqlConnection connection = new SqlConnection(this.connectionString))
                {
                    connection.Open();
                }
            }
            catch
            {
                throw new ProviderException("Check your DB connection!");
            }

            try
            {
                base.Initialize(name, config);

                this.currentDomainName = ADUtil.getDomainName();
                this.populateDomainNamesConversionMaps();

                this.providerName = name;
            }
            catch (Exception ex)
            {
                this.Log(ex.Message, MethodBase.GetCurrentMethod().Name);
                throw new ProviderException(ex.Message);
            }
        }
예제 #2
0
        public override bool ValidateUser(string username, string password)
        {
            if (this.UserExistsSQLServer(username))
            {
                try
                {
                    using (SqlConnection connection = new SqlConnection(this.connectionString))
                    {
                        using (SqlCommand cmd = new SqlCommand("SELECT Count(*) FROM Users WHERE Username = @Username AND Password = @Password", connection))
                        {
                            cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = username;
                            cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = password;
                            connection.Open();

                            return(((int)cmd.ExecuteScalar()) > 0);
                        }
                    }
                }
                catch (Exception e)
                {
                    this.Log(e.Message, MethodBase.GetCurrentMethod().Name);
                    throw new ProviderException(e.Message);
                }
            }

            bool   flag   = false;
            string domain = null;
            string name   = "";

            try
            {
                ADUtil.splitDomainAndName(username, out domain, out name);
                if ((domain == null) || domain.Equals(""))
                {
                    domain = this.dnsroot2NETBIOSName[this.currentDomainName];
                }
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, domain, this.userNameAD, this.passwordAD))
                {
                    flag = context.ValidateCredentials(name, password);
                }
            }
            catch (Exception e)
            {
                this.Log(e.Message, MethodBase.GetCurrentMethod().Name);
                throw new ProviderException(e.Message);
            }
            return(flag);
        }
예제 #3
0
        public override string GetFullyQualifiedRolename(string roleName)
        {
            this.Log("roleName:" + roleName, MethodBase.GetCurrentMethod().Name);

            if (this.RoleExistsSQLServer(roleName))
            {
                return(roleName);
            }

            string domain = null;
            string name   = null;
            string str3   = null;

            try
            {
                ADUtil.splitDomainAndName(roleName, out domain, out name);
                if ((domain == null) || domain.Equals(""))
                {
                    domain = this.dnsroot2NETBIOSName[this.currentDomainName];
                }
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, domain, this.userNameAD, this.passwordAD))
                {
                    using (GroupPrincipal principal = GroupPrincipal.FindByIdentity(context, name))
                    {
                        if ((principal != null) && (principal.DistinguishedName != null))
                        {
                            str3 = this.ncname2NETBIOSName[ADUtil.getDCcomponent(principal.DistinguishedName)] + @"\" + principal.SamAccountName;
                        }
                    }
                    return(str3);
                }
            }
            catch (Exception exception)
            {
                this.Log(exception.Message, MethodBase.GetCurrentMethod().Name);
                throw new ProviderException("Getting fully qualified name for role '" + roleName + "' failed.", exception);
            }
        }
예제 #4
0
        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            this.Log("username " + username, MethodBase.GetCurrentMethod().Name);

            if (this.UserExistsSQLServer(username))
            {
                MembershipUser u = null;

                try
                {
                    using (SqlConnection connection = new SqlConnection(this.connectionString))
                    {
                        using (SqlCommand cmd = new SqlCommand("SELECT Id, Username FROM Users WHERE Username = @Username", connection))
                        {
                            cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = username;
                            connection.Open();

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                if (reader.HasRows)
                                {
                                    reader.Read();
                                    u = this.GetUserByReader(reader);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    this.Log(e.Message, MethodBase.GetCurrentMethod().Name);
                    throw new ProviderException(e.Message);
                }

                return(u);
            }


            string         domain = null;
            string         name   = null;
            MembershipUser user   = null;

            try
            {
                ADUtil.splitDomainAndName(username, out domain, out name);
                if ((domain == null) || domain.Equals(""))
                {
                    domain = this.dnsroot2NETBIOSName[this.currentDomainName];
                }
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, domain, this.userNameAD, this.passwordAD))
                {
                    using (UserPrincipal principal = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, name))
                    {
                        if (principal != null)
                        {
                            user = new MembershipUser(this.providerName, this.ncname2NETBIOSName[ADUtil.getDCcomponent(principal.DistinguishedName)] + @"\" + principal.SamAccountName, null, principal.EmailAddress, null, principal.Name, false, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
                        }
                    }
                    return(user);
                }
            }
            catch (Exception exception)
            {
                this.Log(exception.Message, MethodBase.GetCurrentMethod().Name);
                throw new ProviderException("Getting user '" + username + "' failed." + exception.Message, exception);
            }
        }
예제 #5
0
        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            this.Log("username:"******"Parameters pageIndex and pageSize cannot be negative.");
            }
            if (pageSize < 1)
            {
                throw new ArgumentException("Parameter pageSize should be greater than or equal to 1.");
            }
            MembershipUserCollection users = new MembershipUserCollection();
            int num = 0;

            MembershipUser user = null;

            totalRecords = -1;
            int    partialTotalRecords;
            string domain = null;
            string name   = null;

            try
            {
                ADUtil.splitDomainAndName(usernameToMatch, out domain, out name);
                if ((domain == null) || domain.Equals(""))
                {
                    domain = this.dnsroot2NETBIOSName[this.currentDomainName];
                }
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, domain, this.userNameAD, this.passwordAD))
                {
                    using (UserPrincipal principal = new UserPrincipal(context))
                    {
                        principal.SamAccountName = name + "*";
                        using (PrincipalSearcher searcher = new PrincipalSearcher())
                        {
                            searcher.QueryFilter = principal;
                            using (PrincipalSearchResult <Principal> result = searcher.FindAll())
                            {
                                //users = new MembershipUserCollection();

                                try
                                {
                                    foreach (UserPrincipal principal2 in result)
                                    {
                                        if (principal2 != null)
                                        {
                                            num++;
                                            if (num > ((pageIndex + 1) * pageSize))
                                            {
                                                totalRecords = result.Count <Principal>();
                                                return(users);
                                            }
                                            if (num > (pageIndex * pageSize))
                                            {
                                                if ((principal2.DistinguishedName != null) && !principal2.DistinguishedName.Equals(""))
                                                {
                                                    user = new MembershipUser(this.providerName, this.ncname2NETBIOSName[ADUtil.getDCcomponent(principal2.DistinguishedName)] + @"\" + principal2.SamAccountName, null, principal2.EmailAddress, null, principal2.Name, false, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
                                                    users.Add(user);
                                                }
                                                principal2.Dispose();
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    throw new ProviderException("Finding users by name using filter '" + usernameToMatch + "' failed." + ex.Message, ex);
                                }

                                partialTotalRecords = result.Count <Principal>();
                                //return users;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                this.Log(e.Message, MethodBase.GetCurrentMethod().Name);
                throw;
            }

            //sql server

            try
            {
                using (SqlConnection connection = new SqlConnection(this.connectionString))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT Count(*) FROM Users WHERE Username LIKE @Username", connection))
                    {
                        cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = string.Format(CultureInfo.InvariantCulture, "%{0}%", usernameToMatch);
                        connection.Open();
                        int sqlRecords = (int)cmd.ExecuteScalar();

                        if (sqlRecords <= 0)
                        {
                            totalRecords = partialTotalRecords;
                            return(users);
                        }
                        else
                        {
                            totalRecords = partialTotalRecords + sqlRecords;
                        }
                    }

                    using (SqlCommand cmd = new SqlCommand("SELECT Id, Username FROM Users WHERE Username LIKE @Username ORDER BY Username ASC", connection))
                    {
                        cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = string.Format(CultureInfo.InvariantCulture, "%{0}%", usernameToMatch);

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    num++;
                                    if (num > ((pageIndex + 1) * pageSize))
                                    {
                                        break;
                                    }
                                    if (num > (pageIndex * pageSize))
                                    {
                                        MembershipUser u = this.GetUserByReader(reader);
                                        users.Add(u);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                this.Log(e.Message, MethodBase.GetCurrentMethod().Name);
                throw new ProviderException(e.Message);
            }

            return(users);
        }
예제 #6
0
        public override string[] GetUsersInRole(string roleName)
        {
            this.Log("roleName:" + roleName, MethodBase.GetCurrentMethod().Name);

            if (this.RoleExistsSQLServer(roleName))
            {
                List <string> users = new List <string>();
                try
                {
                    using (SqlConnection connection = new SqlConnection(this.connectionString))
                    {
                        connection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT Users.Username FROM Roles INNER JOIN UsersRoles ON Roles.Id = UsersRoles.IdRole INNER JOIN Users ON UsersRoles.IdUser = Users.Id WHERE (Roles.Rolename = @Rolename) ORDER BY Users.Username", connection))
                        {
                            cmd.Parameters.Add("@Rolename", SqlDbType.NVarChar).Value = roleName;
                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                if (reader.HasRows)
                                {
                                    while (reader.Read())
                                    {
                                        users.Add(reader.GetString(0));
                                    }
                                }
                            }
                        }

                        if (this.useRolesDBforAD)
                        {
                            using (SqlCommand cmd = new SqlCommand("SELECT UsersRolesAD.Username FROM Roles INNER JOIN UsersRolesAD ON Roles.Id = UsersRolesAD.IdRole WHERE Roles.Rolename = @Rolename ORDER BY UsersRolesAD.Username", connection))
                            {
                                cmd.Parameters.Add("@Rolename", SqlDbType.NVarChar).Value = roleName;
                                using (SqlDataReader reader = cmd.ExecuteReader())
                                {
                                    if (reader.HasRows)
                                    {
                                        while (reader.Read())
                                        {
                                            users.Add(reader.GetString(0));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    this.Log(e.Message, MethodBase.GetCurrentMethod().Name);
                    throw new ProviderException(e.Message);
                }

                return(users.ToArray());
            }


            string domain = null;
            string name   = null;
            LinkedList <string> source = new LinkedList <string>();

            try
            {
                ADUtil.splitDomainAndName(roleName, out domain, out name);
                if ((domain == null) || domain.Equals(""))
                {
                    domain = this.dnsroot2NETBIOSName[this.currentDomainName];
                }
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, domain, this.userNameAD, this.passwordAD))
                {
                    using (GroupPrincipal principal = GroupPrincipal.FindByIdentity(context, name))
                    {
                        if (principal == null)
                        {
                            throw new ProviderException("Unable to find role '" + roleName + "'.");
                        }
                        using (PrincipalSearchResult <Principal> result = principal.GetMembers(true))
                        {
                            foreach (Principal principal2 in result)
                            {
                                if (principal2 != null)
                                {
                                    using (principal2)
                                    {
                                        if (principal2 is UserPrincipal)
                                        {
                                            using (UserPrincipal principal3 = (UserPrincipal)principal2)
                                            {
                                                if ((principal3.DistinguishedName != null) && !principal3.DistinguishedName.Equals(""))
                                                {
                                                    source.AddLast(this.ncname2NETBIOSName[ADUtil.getDCcomponent(principal3.DistinguishedName)] + @"\" + principal3.SamAccountName);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                this.Log(exception.Message, MethodBase.GetCurrentMethod().Name);
                throw new ProviderException("Getting users in role '" + roleName + "' failed.", exception);
            }

            return(source.ToArray <string>());
        }
예제 #7
0
        public override string[] GetRolesForUser(string username)
        {
            this.Log("username:"******"SELECT Roles.Rolename FROM Roles INNER JOIN UsersRoles ON Roles.Id = UsersRoles.IdRole INNER JOIN Users ON UsersRoles.IdUser = Users.Id WHERE Users.Username = @Username ORDER BY Roles.Rolename", connection))
                        {
                            cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = username;
                            connection.Open();
                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                if (reader.HasRows)
                                {
                                    while (reader.Read())
                                    {
                                        roles.Add(reader.GetString(0));
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    this.Log(e.Message, MethodBase.GetCurrentMethod().Name);
                    throw new ProviderException(e.Message);
                }

                this.Log("Roles:" + string.Join(",", roles.ToArray()), MethodBase.GetCurrentMethod().Name);
                return(roles.ToArray());
            }

            string domain = null;
            string name   = null;
            LinkedList <string> source = new LinkedList <string>();

            try
            {
                ADUtil.splitDomainAndName(username, out domain, out name);
                if ((domain == null) || domain.Equals(""))
                {
                    domain = this.dnsroot2NETBIOSName[this.currentDomainName];
                }
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, domain, this.userNameAD, this.passwordAD))
                {
                    using (UserPrincipal principal = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, name))
                    {
                        if (principal == null)
                        {
                            throw new ProviderException("Unable to find user '" + username + "'.");
                        }
                        using (PrincipalSearchResult <Principal> result = principal.GetAuthorizationGroups())
                        {
                            using (IEnumerator <Principal> enumerator = result.GetEnumerator())
                            {
                                while (enumerator.MoveNext())
                                {
                                    try
                                    {
                                        using (Principal principal2 = enumerator.Current)
                                        {
                                            if ((principal2 != null) && (principal2 is GroupPrincipal))
                                            {
                                                using (GroupPrincipal principal3 = (GroupPrincipal)principal2)
                                                {
                                                    if ((principal3.DistinguishedName != null) && principal3.IsSecurityGroup.Value)
                                                    {
                                                        source.AddLast(this.ncname2NETBIOSName[ADUtil.getDCcomponent(principal3.DistinguishedName)] + @"\" + principal3.SamAccountName);
                                                    }
                                                }
                                            }
                                        }
                                        continue;
                                    }
                                    catch (Exception)
                                    {
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                this.Log(exception.Message, MethodBase.GetCurrentMethod().Name);
                throw new ProviderException("Getting roles for user '" + username + "' failed.", exception);
            }

            if (this.useRolesDBforAD)
            {
                try
                {
                    using (SqlConnection connection = new SqlConnection(this.connectionString))
                    {
                        using (SqlCommand cmd = new SqlCommand("SELECT Roles.Rolename FROM Roles INNER JOIN UsersRolesAD ON Roles.Id = UsersRolesAD.IdRole WHERE UsersRolesAD.Username = @Username ORDER BY Roles.Rolename", connection))
                        {
                            cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = username;
                            connection.Open();
                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                if (reader.HasRows)
                                {
                                    while (reader.Read())
                                    {
                                        source.AddLast(reader.GetString(0));
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    this.Log(e.Message, MethodBase.GetCurrentMethod().Name);
                    throw new ProviderException("Getting roles for user '" + username + "' failed.", e);
                }
            }

            this.Log("Roles:" + string.Join(",", source.ToArray <string>()), MethodBase.GetCurrentMethod().Name);
            return(source.ToArray <string>());
        }
예제 #8
0
        public override string[] GetAllRoles(string rolenameToMatch)
        {
            this.Log("rolenameToMatch:" + rolenameToMatch, MethodBase.GetCurrentMethod().Name);

            string domain = null;
            string name   = null;
            LinkedList <string> source = new LinkedList <string>();

            try
            {
                ADUtil.splitDomainAndName(rolenameToMatch, out domain, out name);
                if ((domain == null) || domain.Equals(""))
                {
                    domain = this.dnsroot2NETBIOSName[this.currentDomainName];
                }
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, domain, this.userNameAD, this.passwordAD))
                {
                    using (GroupPrincipal principal = new GroupPrincipal(context))
                    {
                        principal.SamAccountName  = name + "*";
                        principal.IsSecurityGroup = true;
                        using (PrincipalSearcher searcher = new PrincipalSearcher())
                        {
                            searcher.QueryFilter = principal;
                            using (PrincipalSearchResult <Principal> result = searcher.FindAll())
                            {
                                try
                                {
                                    foreach (GroupPrincipal principal2 in result)
                                    {
                                        if (principal2 != null)
                                        {
                                            if ((principal2.DistinguishedName != null) && !principal2.DistinguishedName.Equals(""))
                                            {
                                                source.AddLast(this.ncname2NETBIOSName[ADUtil.getDCcomponent(principal2.DistinguishedName)] + @"\" + principal2.SamAccountName);
                                            }
                                            principal2.Dispose();
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                this.Log(exception.Message, MethodBase.GetCurrentMethod().Name);
                throw new ProviderException("Getting all roles with filter '" + rolenameToMatch + "' failed.", exception);
            }

            //add roles sql server
            List <string> roles = new List <string>();

            try
            {
                using (SqlConnection connection = new SqlConnection(this.connectionString))
                {
                    connection.Open();
                    using (SqlCommand cmd = new SqlCommand("SELECT Rolename FROM Roles ORDER BY Rolename", connection))
                    {
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    roles.Add(reader.GetString(0));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                this.Log(e.Message, MethodBase.GetCurrentMethod().Name);
                throw new ProviderException(e.Message);
            }

            string[] rolesSQL = roles.ToArray();

            return(source.Concat(roles).ToArray());
        }