Exemplo n.º 1
0
        /// <summary>
        /// Queries a string-valued context attribute by the named attribute.
        /// </summary>
        /// <param name="attrib">The string-valued attribute to query.</param>
        /// <returns></returns>
        private string QueryContextString(ContextQueryAttrib attrib)
        {
            SecPkgContext_String stringAttrib;
            SecurityStatus       status = SecurityStatus.InternalError;
            string result = null;
            bool   gotRef = false;

            if (attrib != ContextQueryAttrib.Names && attrib != ContextQueryAttrib.Authority)
            {
                throw new InvalidOperationException("QueryContextString can only be used to query context Name and Authority attributes");
            }

            stringAttrib = new SecPkgContext_String();

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                this.ContextHandle.DangerousAddRef(ref gotRef);
            }
            catch (Exception)
            {
                if (gotRef)
                {
                    this.ContextHandle.DangerousRelease();
                    gotRef = false;
                }
                throw;
            }
            finally
            {
                if (gotRef)
                {
                    status = ContextNativeMethods.QueryContextAttributes_String(
                        ref this.ContextHandle.rawHandle,
                        attrib,
                        ref stringAttrib
                        );

                    this.ContextHandle.DangerousRelease();

                    if (status == SecurityStatus.OK)
                    {
                        result = Marshal.PtrToStringUni(stringAttrib.StringResult);
                        ContextNativeMethods.FreeContextBuffer(stringAttrib.StringResult);
                    }
                }
            }

            if (status == SecurityStatus.Unsupported)
            {
                return(null);
            }
            else if (status != SecurityStatus.OK)
            {
                throw new SSPIException("Failed to query the context's associated user name", status);
            }

            return(result);
        }
Exemplo n.º 2
0
 internal static extern SecurityStatus QueryContextAttributes_String(
     ref RawSspiHandle contextHandle,
     ContextQueryAttrib attrib,
     ref SecPkgContext_String names
     );