Example #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(CContextQueryAttrib attrib)
        {
            CSecPkgContext_String stringAttrib;
            CSecurityStatus       status = CSecurityStatus.InternalError;
            string result = null;
            bool   gotRef = false;

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

            stringAttrib = new CSecPkgContext_String();

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

                    this.ContextHandle.DangerousRelease();

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

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

            return(result);
        }
Example #2
0
 internal static extern CSecurityStatus QueryContextAttributes_String(
     ref CRawSspiHandle contextHandle,
     CContextQueryAttrib attrib,
     ref CSecPkgContext_String names
     );