Beispiel #1
0
        /// <summary>
        /// Try and parse an SPN string to a class.
        /// </summary>
        /// <param name="spn">The SPN string.</param>
        /// <param name="result">The result class.</param>
        /// <returns>True if the SPN was parsed successfully.</returns>
        /// <exception cref="FormatException">Thrown in invalid SPN.</exception>
        public static bool TryParse(string spn, out ServicePrincipalName result)
        {
            result = null;

            OptionalInt32  cServiceClass = 1;
            StringBuilder  ServiceClass  = new StringBuilder(1);
            OptionalInt32  cServiceName  = 1;
            StringBuilder  ServiceName   = new StringBuilder(1);
            OptionalInt32  cInstanceName = 1;
            StringBuilder  InstanceName  = new StringBuilder(1);
            OptionalUInt16 InstancePort  = 0;

            var err = SecurityNativeMethods.DsCrackSpn(spn, cServiceClass, ServiceClass,
                                                       cServiceName, ServiceName, cInstanceName, InstanceName, InstancePort);

            if (err != Win32Error.ERROR_BUFFER_OVERFLOW)
            {
                return(false);
            }

            ServiceClass = new StringBuilder(cServiceClass.Value);
            ServiceName  = new StringBuilder(cServiceName.Value);
            InstanceName = new StringBuilder(cInstanceName.Value);

            if (SecurityNativeMethods.DsCrackSpn(spn, cServiceClass, ServiceClass,
                                                 cServiceName, ServiceName, cInstanceName, InstanceName, InstancePort) != Win32Error.SUCCESS)
            {
                return(false);
            }

            result = new ServicePrincipalName()
            {
                ServiceClass = ServiceClass.ToString(),
                ServiceName  = ServiceName.ToString(),
                InstanceName = InstanceName.ToString(),
                InstancePort = InstancePort.Value
            };

            return(true);
        }