diff --git a/NSspi/EnumMgr.cs b/NSspi/EnumMgr.cs new file mode 100644 index 0000000..cdc0633 --- /dev/null +++ b/NSspi/EnumMgr.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; + +namespace NSspi +{ + [AttributeUsage( AttributeTargets.Enum )] + public class EnumStringAttribute : Attribute + { + public EnumStringAttribute( string text ) + { + this.Text = text; + } + + public string Text { get; private set; } + } + + public class EnumMgr + { + public static string Text( Enum value ) + { + FieldInfo field = value.GetType().GetField( value.ToString() ); + + EnumStringAttribute[] attribs = (EnumStringAttribute[])field.GetCustomAttributes( typeof( EnumStringAttribute ), false ); + + if( attribs == null || attribs.Length == 0 ) + { + return null; + } + else + { + return attribs[0].Text; + } + } + + public static T Enum( string text ) + { + FieldInfo[] fields = typeof( T ).GetFields(); + + EnumStringAttribute[] attribs; + + foreach( FieldInfo field in fields ) + { + attribs = (EnumStringAttribute[])field.GetCustomAttributes( typeof( EnumStringAttribute ), false ); + + foreach( EnumStringAttribute attrib in attribs ) + { + if( attrib.Text == text ) + { + return (T)field.GetValue( null ); + } + } + } + + throw new ArgumentException( "Could not find a matching enumeration value for the text '" + text + "'." ); + } + } +} diff --git a/NSspi/NSspi.csproj b/NSspi/NSspi.csproj index f53933b..03b6169 100644 --- a/NSspi/NSspi.csproj +++ b/NSspi/NSspi.csproj @@ -59,6 +59,7 @@ + diff --git a/NSspi/SSPIException.cs b/NSspi/SSPIException.cs index 1b49e2d..e2e466f 100644 --- a/NSspi/SSPIException.cs +++ b/NSspi/SSPIException.cs @@ -70,7 +70,12 @@ namespace NSspi { get { - return string.Format( "{0}. Error Code = '{1:X}'.", this.message, this.errorCode ); + return string.Format( + "{0}. Error Code = '{1:X}' - \"{2}\".", + this.message, + this.errorCode, + EnumMgr.Text(this.errorCode) + ); } } } diff --git a/NSspi/SecurityStatus.cs b/NSspi/SecurityStatus.cs index e3eef6c..55cc9dc 100644 --- a/NSspi/SecurityStatus.cs +++ b/NSspi/SecurityStatus.cs @@ -30,56 +30,100 @@ namespace NSspi /// /// The request completed successfully /// + [EnumString( "No error" )] OK = 0x00000000, /// /// The token returned by the context needs to be provided to the cooperating party /// to continue construction of the context. /// - ContinueNeeded = 0x00090312, + [EnumString( "Authentication cycle needs to continue" )] + ContinueNeeded = 0x00090312, /// /// Occurs after a client calls InitializeSecurityContext to indicate that the client /// must call CompleteAuthToken. /// + [EnumString( "Authentication cycle needs to perform a 'complete'." )] CompleteNeeded = 0x00090313, /// /// Occurs after a client calls InitializeSecurityContext to indicate that the client /// must call CompleteAuthToken and pass the result to the server. /// - CompAndContinue = 0x00090314, + [EnumString( "Authentication cycle needs to perform a 'complete' and then continue." )] + CompAndContinue = 0x00090314, /// /// An attempt to use the context was performed after the context's expiration time elapsed. /// - ContextExpired = 0x00090317, - CredentialsNeeded = 0x00090320, + [EnumString( "The security context was used after its expiration time passed." )] + ContextExpired = 0x00090317, + + [EnumString( "The credentials supplied to the security context were not fully initialized." )] + CredentialsNeeded = 0x00090320, + + [EnumString( "The context data must be re-negotiated with the peer" )] Renegotiate = 0x00090321, // Errors + [EnumString( "Not enough memory.")] OutOfMemory = 0x80090300, + + [EnumString( "The handle provided to the API was invalid.")] InvalidHandle = 0x80090301, + + [EnumString( "The attempted operation is not supported")] Unsupported = 0x80090302, + + [EnumString( "The specified principle is not known in the authentication system.")] TargetUnknown = 0x80090303, + + [EnumString( "An internal error occurred" )] InternalError = 0x80090304, /// /// No security provider package was found with the given name. /// + [EnumString( "The requested security package was not found.")] PackageNotFound = 0x80090305, NotOwner = 0x80090306, CannotInstall = 0x80090307, + + /// + /// A token was provided that contained incorrect or corrupted data. + /// + [EnumString("The provided authentication token is invalid or corrupted.")] InvalidToken = 0x80090308, + CannotPack = 0x80090309, QopNotSupported = 0x8009030A, + + /// + /// Impersonation is not supported. + /// + [EnumString("Impersonation is not supported with the current security package.")] NoImpersonation = 0x8009030B, + + [EnumString("The logon was denied, perhaps because the provided credentials were incorrect.")] LogonDenied = 0x8009030C, + + + [EnumString( "The credentials provided are not recognized by the selected security package.")] UnknownCredentials = 0x8009030D, + + [EnumString( "No credentials are available in the selected security package.")] NoCredentials = 0x8009030E, + + [EnumString( "A message that was provided to the Decrypt or VerifySignature functions was altered " + + "after it was created.")] MessageAltered = 0x8009030F, + + [EnumString( "A message was received out of the expected order.")] OutOfSequence = 0x80090310, + + [EnumString( "The current security package cannot contact an authenticating authority.")] NoAuthenticatingAuthority = 0x80090311, /// diff --git a/NsspiDemo/Program.cs b/NsspiDemo/Program.cs index cd8a636..fede64f 100644 --- a/NsspiDemo/Program.cs +++ b/NsspiDemo/Program.cs @@ -9,6 +9,9 @@ namespace NSspi { public static void Main( string[] args ) { + EnumMgr.Text( SecurityStatus.AlgorithmMismatch ); + return; + CredTest( PackageNames.Negotiate ); CredTest( PackageNames.Kerberos ); CredTest( PackageNames.Ntlm );