reissaus73
Goto Top

Mit Java SID des angemeldeten Benutzers auslesen und in Variable schreiben

Wer kann mir bei folgenden Problem helfen.

Wie kann man die SID des angemeldeten Users in Java auslesen und in eine Variable schreiben.
Hilfreicher wäre nartürlich das auslesen der SID über AD mit Hilfe der UserID, ebenfalls über JAVA.

Wie es mit Powershell, C# oder CMD geht ist mir bekannt, ich brauche es leider in JAVA.


Ich danke Euch schonmal im voraus.

Content-Key: 336662

Url: https://administrator.de/contentid/336662

Printed on: April 24, 2024 at 18:04 o'clock

Mitglied: 132895
Solution 132895 May 02, 2017 updated at 12:00:11 (UTC)
Goto Top
com.sun.security.auth.module.NTSystem ntsys = new com.sun.security.auth.module.NTSystem();
String sid = ntsys.getUserSID();
System.out.println(sid);
https://docs.oracle.com/javase/7/docs/jre/api/security/jaas/spec/com/sun ...

Oder wenn es eine LDAP Abfrage im AD sein soll:
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class searchSIDViaLDAP {
    public static void main(String args) {
        String SID = GetADUserSID("Administrator@domain.tld","Passw0rd","dc01.domain.tld","DC=domain,DC=tld","maxmuster");  
        System.out.println(SID);
    }

    public static String GetADUserSID(String username,String password,String DC,String DomainDN,String strSearchUser){
        Properties properties = new Properties();
        DirContext dirContext;
        SearchControls searchCtls = new SearchControls();
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        searchCtls.setReturningAttributes(new String{"objectSid"});  
        String sFilter = "(&(objectCategory=Person)(objectClass=User)(sAMAccountname=" + strSearchUser + "))";  
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");  
        properties.put(Context.PROVIDER_URL, "LDAP://" + DC);  
        properties.put(Context.SECURITY_PRINCIPAL, username);
        properties.put(Context.SECURITY_CREDENTIALS, password);
        properties.put("java.naming.ldap.attributes.binary", "objectSid");  
        try {
            dirContext = new InitialDirContext(properties);
            NamingEnumeration<SearchResult> result = dirContext.search(DomainDN,sFilter,searchCtls);
            if (result.hasMore()){
                byte bSID = (byte)result.next().getAttributes().get("objectSID").get();  
                return convertSidToStr(bSID);
            }
            dirContext.close();
        } catch (NamingException e) {
            System.out.println("Error:" + e.getMessage());  
            return "";  
        }
        return "";  
    }
    public static String convertSidToStr(byte sid) {
        if (sid==null) return null;
        if (sid.length<8 || sid.length % 4 != 0) return "";  
        StringBuilder sb = new StringBuilder();
        sb.append("S-").append(sid);  
        int c = sid[1];
        ByteBuffer bb = ByteBuffer.wrap(sid);
        sb.append("-").append((long)bb.getLong() & 0XFFFFFFFFFFFFL);  
        bb.order(ByteOrder.LITTLE_ENDIAN);
        for (int i=0; i<c; i++) {
            sb.append("-").append((long)bb.getInt() & 0xFFFFFFFFL);  
        }
        return sb.toString();
    }
}
Gruß
Member: reissaus73
reissaus73 May 02, 2017 at 12:17:57 (UTC)
Goto Top
Super ich danke dir.
Hatte leider nichts brauchbares gefunden.