// static methods
        public static SecurityContext Initialize(SecurityCredential credential, string servicePrincipalName, byte[] input, out byte[] output)
        {
            var context = new SecurityContext();
            context._credential = credential;

            context.Initialize(servicePrincipalName, input, out output);
            return context;
        }
示例#2
0
        // static methods
        public static SecurityContext Initialize(SecurityCredential credential, string servicePrincipalName, byte[] input, out byte[] output)
        {
            var context = new SecurityContext();

            context._credential = credential;

            context.Initialize(servicePrincipalName, input, out output);
            return(context);
        }
        // public methods
        public static SecurityCredential Acquire(SspiPackage package, string username, SecureString password)
        {
            long timestamp;

            var credential = new SecurityCredential();

#if NET452
            RuntimeHelpers.PrepareConstrainedRegions();
#endif
            try { }
            finally
            {
                uint result;
                if (password == null)
                {
                    result = NativeMethods.AcquireCredentialsHandle(
                        null,
                        package.ToString(),
                        SecurityCredentialUse.Outbound,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        0,
                        IntPtr.Zero,
                        ref credential._sspiHandle,
                        out timestamp);
                }
                else
                {
                    using (var authIdentity = new AuthIdentity(username, password))
                    {
                        // TODO: make this secure by using SecurePassword
                        result = NativeMethods.AcquireCredentialsHandle(
                            null,
                            package.ToString(),
                            SecurityCredentialUse.Outbound,
                            IntPtr.Zero,
                            authIdentity,
                            0,
                            IntPtr.Zero,
                            ref credential._sspiHandle,
                            out timestamp);
                    }
                }
                if (result != NativeMethods.SEC_E_OK)
                {
                    credential.SetHandleAsInvalid();
                    throw NativeMethods.CreateException(result, "Unable to acquire credential.");
                }
            }
            return(credential);
        }
        // public methods
        public static SecurityCredential Acquire(SspiPackage package, string username, SecureString password)
        {
            long timestamp;

            var credential = new SecurityCredential();
#if NET45
            RuntimeHelpers.PrepareConstrainedRegions();
#endif
            try { }
            finally
            {
                uint result;
                if (password == null)
                {
                    result = NativeMethods.AcquireCredentialsHandle(
                        null,
                        package.ToString(),
                        SecurityCredentialUse.Outbound,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        0,
                        IntPtr.Zero,
                        ref credential._sspiHandle,
                        out timestamp);
                }
                else
                {
                    using(var authIdentity = new AuthIdentity(username, password))
                    {
                        // TODO: make this secure by using SecurePassword
                        result = NativeMethods.AcquireCredentialsHandle(
                            null,
                            package.ToString(),
                            SecurityCredentialUse.Outbound,
                            IntPtr.Zero,
                            authIdentity,
                            0,
                            IntPtr.Zero,
                            ref credential._sspiHandle,
                            out timestamp);
                    }
                }
                if (result != NativeMethods.SEC_E_OK)
                {
                    credential.SetHandleAsInvalid();
                    throw NativeMethods.CreateException(result, "Unable to acquire credential.");
                }
            }
            return credential;
        }