Added rough support for attaching messages to errors.

This commit is contained in:
antiduh
2014-07-11 21:29:17 +00:00
parent b65b6c6abe
commit 50efc7702b
5 changed files with 118 additions and 5 deletions

60
NSspi/EnumMgr.cs Normal file
View File

@@ -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<T>( 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 + "'." );
}
}
}

View File

@@ -59,6 +59,7 @@
<Compile Include="Contexts\ImpersonationHandle.cs" />
<Compile Include="Contexts\SafeContextHandle.cs" />
<Compile Include="Credentials\CurrentCredential.cs" />
<Compile Include="EnumMgr.cs" />
<Compile Include="SecPkgInfo.cs" />
<Compile Include="Contexts\ServerContext.cs" />
<Compile Include="Credentials\ClientCredential.cs" />

View File

@@ -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)
);
}
}
}

View File

@@ -30,56 +30,100 @@ namespace NSspi
/// <summary>
/// The request completed successfully
/// </summary>
[EnumString( "No error" )]
OK = 0x00000000,
/// <summary>
/// The token returned by the context needs to be provided to the cooperating party
/// to continue construction of the context.
/// </summary>
ContinueNeeded = 0x00090312,
[EnumString( "Authentication cycle needs to continue" )]
ContinueNeeded = 0x00090312,
/// <summary>
/// Occurs after a client calls InitializeSecurityContext to indicate that the client
/// must call CompleteAuthToken.
/// </summary>
[EnumString( "Authentication cycle needs to perform a 'complete'." )]
CompleteNeeded = 0x00090313,
/// <summary>
/// Occurs after a client calls InitializeSecurityContext to indicate that the client
/// must call CompleteAuthToken and pass the result to the server.
/// </summary>
CompAndContinue = 0x00090314,
[EnumString( "Authentication cycle needs to perform a 'complete' and then continue." )]
CompAndContinue = 0x00090314,
/// <summary>
/// An attempt to use the context was performed after the context's expiration time elapsed.
/// </summary>
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,
/// <summary>
/// No security provider package was found with the given name.
/// </summary>
[EnumString( "The requested security package was not found.")]
PackageNotFound = 0x80090305,
NotOwner = 0x80090306,
CannotInstall = 0x80090307,
/// <summary>
/// A token was provided that contained incorrect or corrupted data.
/// </summary>
[EnumString("The provided authentication token is invalid or corrupted.")]
InvalidToken = 0x80090308,
CannotPack = 0x80090309,
QopNotSupported = 0x8009030A,
/// <summary>
/// Impersonation is not supported.
/// </summary>
[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,
/// <summary>

View File

@@ -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 );