Added documentation

pull/83/head
David Hall 2019-11-05 16:20:52 -07:00
parent b7729753da
commit 76849b7f6f
1 changed files with 78 additions and 107 deletions

View File

@ -5,8 +5,6 @@ using System.Runtime.Serialization;
using Vanara.PInvoke;
using static Vanara.PInvoke.Authz;
// ReSharper disable InconsistentNaming
namespace Microsoft.Samples.DynamicAccessControl
{
/// <summary>Enumeration used to identify if a ClaimValueDictionary comprised of user or device claims.</summary>
@ -20,59 +18,77 @@ namespace Microsoft.Samples.DynamicAccessControl
[Serializable]
public class BadValueException : Exception
{
public BadValueException() { }
/// <summary>Initializes a new instance of the <see cref="BadValueException"/> class.</summary>
public BadValueException()
{
}
public BadValueException(string message) : base(message) { }
/// <summary>Initializes a new instance of the <see cref="BadValueException"/> class.</summary>
/// <param name="message">The message that describes the error.</param>
public BadValueException(string message) : base(message)
{
}
public BadValueException(string message, Exception innerException) : base(message, innerException) { }
/// <summary>Initializes a new instance of the <see cref="BadValueException"/> class.</summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">
/// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.
/// </param>
public BadValueException(string message, Exception innerException) : base(message, innerException)
{
}
protected BadValueException(SerializationInfo info, StreamingContext context) : base(info, context) { }
/// <summary>Initializes a new instance of the <see cref="BadValueException"/> class.</summary>
/// <param name="info">
/// The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception
/// being thrown.
/// </param>
/// <param name="context">
/// The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.
/// </param>
protected BadValueException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
/// <summary>
/// Class to represent the type of claims values held, the value(s) and obtain native (unmanaged) pointers to the value as they are stored in the union
/// members of AUTHZ_SECURITY_ATTRIBUTE_V1 structure's 'Values' field.
/// Class to represent the type of claims values held, the value(s) and obtain native (unmanaged) pointers to the value as they are
/// stored in the union members of AUTHZ_SECURITY_ATTRIBUTE_V1 structure's 'Values' field.
/// </summary>
public class ClaimValue
{
internal AUTHZ_SECURITY_ATTRIBUTE_V1 attr;
public ClaimValue(string value)
{
attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, value);
}
/// <summary>Initializes a new instance of the <see cref="ClaimValue"/> class based on a string.</summary>
/// <param name="value">The string value.</param>
public ClaimValue(string value) => attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, value);
public ClaimValue(ulong version, string fullyQualifiedBinaryName)
{
attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, new AUTHZ_SECURITY_ATTRIBUTE_FQBN_VALUE { pName = fullyQualifiedBinaryName, Version = version });
}
/// <summary>Initializes a new instance of the <see cref="ClaimValue"/> class based on a fully qualified binary name and version.</summary>
/// <param name="version">The version.</param>
/// <param name="fullyQualifiedBinaryName">Name of the fully qualified binary.</param>
public ClaimValue(ulong version, string fullyQualifiedBinaryName) => attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, new AUTHZ_SECURITY_ATTRIBUTE_FQBN_VALUE { pName = fullyQualifiedBinaryName, Version = version });
public ClaimValue(string[] value)
{
attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, value);
}
/// <summary>Initializes a new instance of the <see cref="ClaimValue"/> class based on multiple string values.</summary>
/// <param name="value">The string values.</param>
public ClaimValue(string[] value) => attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, value);
public ClaimValue(byte[] value)
{
attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, new AUTHZ_SECURITY_ATTRIBUTE_OCTET_STRING_VALUE { pValue = value, ValueLength = (uint)value.Length });
}
/// <summary>Initializes a new instance of the <see cref="ClaimValue"/> class based on a binary blob.</summary>
/// <param name="value">The value.</param>
public ClaimValue(byte[] value) => attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, new AUTHZ_SECURITY_ATTRIBUTE_OCTET_STRING_VALUE { pValue = value, ValueLength = (uint)value.Length });
public ClaimValue(ulong value)
{
attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, value);
}
/// <summary>Initializes a new instance of the <see cref="ClaimValue"/> class based on an unsigned long value.</summary>
/// <param name="value">The value.</param>
public ClaimValue(ulong value) => attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, value);
public ClaimValue(long value)
{
attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, value);
}
/// <summary>Initializes a new instance of the <see cref="ClaimValue"/> class based on a long value.</summary>
/// <param name="value">The value.</param>
public ClaimValue(long value) => attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, value);
public ClaimValue(bool value)
{
attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, value);
}
/// <summary>Initializes a new instance of the <see cref="ClaimValue"/> class based on a boolean value.</summary>
/// <param name="value">if set to <see langword="true"/> [value].</param>
public ClaimValue(bool value) => attr = new AUTHZ_SECURITY_ATTRIBUTE_V1(null, value);
/// <summary>Get the number of values contained in the Microsoft.Samples.Cbac.ClaimValue</summary>
/// <summary>Get the number of values contained in the object.</summary>
public uint ValueCount => attr.ValueCount;
}
@ -85,14 +101,19 @@ namespace Microsoft.Samples.DynamicAccessControl
/// <summary>Identifies if this instance represents user's claims or device's claims</summary>
/// <param name="type">ClaimDefinitionType.User to indicate user's claims and ClaimDefinitionType.Device to indicate device's claims.</param>
/// <remarks>
/// When ClaimDefinitionType.User, AithzModifyClaims in invoked with SidClass AuthzContextInfoUserClaims and when ClaimDefinitionType.Device with
/// SidClass AuthzContextInfoDeviceClaims.
/// When ClaimDefinitionType.User, AithzModifyClaims in invoked with SidClass AuthzContextInfoUserClaims and when
/// ClaimDefinitionType.Device with SidClass AuthzContextInfoDeviceClaims.
/// </remarks>
public ClaimValueDictionary(ClaimDefinitionType type)
{
claimDefnType = type;
}
public ClaimValueDictionary(ClaimDefinitionType type) => claimDefnType = type;
/// <summary>Initializes a new instance of the <see cref="ClaimValueDictionary"/> class.</summary>
/// <param name="info">
/// A <see cref="T:System.Runtime.Serialization.SerializationInfo"/> object containing the information required to serialize the <see cref="T:System.Collections.Generic.Dictionary`2"/>.
/// </param>
/// <param name="context">
/// A <see cref="T:System.Runtime.Serialization.StreamingContext"/> structure containing the source and destination of the
/// serialized stream associated with the <see cref="T:System.Collections.Generic.Dictionary`2"/>.
/// </param>
protected ClaimValueDictionary(SerializationInfo info, StreamingContext context)
: base(info, context)
{
@ -100,8 +121,8 @@ namespace Microsoft.Samples.DynamicAccessControl
/// <summary>Adds or replaces claims in the specified Authz Client Context.</summary>
/// <remarks>
/// This method invokes AuthzModifyClaims, modifying the claims using AUTHZ_SECURITY_ATTRIBUTE_OPERATION_REPLACE. This ensures that the values of a
/// claims that already exists are replaces and the ones not present are added.
/// This method invokes AuthzModifyClaims, modifying the claims using AUTHZ_SECURITY_ATTRIBUTE_OPERATION_REPLACE. This ensures that
/// the values of a claims that already exists are replaces and the ones not present are added.
/// </remarks>
/// <param name="handleClientContext">Handle to the Authz Client Context to be modified</param>
/// <returns>Win32Error.ERROR_SUCCESS on success and Win32 error code otherwise.</returns>
@ -134,6 +155,18 @@ namespace Microsoft.Samples.DynamicAccessControl
return Win32Error.ERROR_SUCCESS;
}
/// <summary>
/// Implements the <see cref="T:System.Runtime.Serialization.ISerializable"/> interface and returns the data needed to serialize the
/// <see cref="T:System.Collections.Generic.Dictionary`2"/> instance.
/// </summary>
/// <param name="info">
/// A <see cref="T:System.Runtime.Serialization.SerializationInfo"/> object that contains the information required to serialize the
/// <see cref="T:System.Collections.Generic.Dictionary`2"/> instance.
/// </param>
/// <param name="context">
/// A <see cref="T:System.Runtime.Serialization.StreamingContext"/> structure that contains the source and destination of the
/// serialized stream associated with the <see cref="T:System.Collections.Generic.Dictionary`2"/> instance.
/// </param>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
@ -143,67 +176,5 @@ namespace Microsoft.Samples.DynamicAccessControl
info.AddValue("claimDefnType", claimDefnType);
}
}
/*static class
{
[StructLayout(LayoutKind.Sequential)]
public struct AUTHZ_SECURITY_ATTRIBUTES_INFORMATION
{
public USHORT Version;
public USHORT Reserved;
public uint AttributeCount;
public PAUTHZ_SECURITY_ATTRIBUTE_V1 pAttributeV1;
}
public enum AuthzSecurityAttributeValueType : ushort
{
Invalid = 0x0,
Int = 0x1,
String = 0x3,
Boolean = 0x6,
}
[Flags]
public enum AuthzSecurityAttributeFlags : uint // uint
{
None = 0x0,
NonInheritable = 0x1,
ValueCaseSensitive = 0x2,
}
[StructLayout(LayoutKind.Sequential)]
public struct AUTHZ_SECURITY_ATTRIBUTE_V1
{
[MarshalAs(UnmanagedType.LPWStr)] public string Name;
public AuthzSecurityAttributeValueType Type;
public USHORT Reserved;
public AuthzSecurityAttributeFlags Flags;
public uint ValueCount;
public IntPtr Values;
}
public enum AuthzContextInformationClass : uint
{
AuthzContextInfoUserClaims = 13,
AuthzContextInfoDeviceClaims,
};
public enum AuthzSecurityAttributeOperation : uint
{
None = 0,
ReplaceAll,
Add,
Delete,
Replace
}
[DllImport(Win32.AUTHZ_DLL, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AuthzModifyClaims(
AUTHZ_CLIENT_CONTEXT_HANDLE handleClientContext,
AuthzContextInformationClass infoClass,
AuthzSecurityAttributeOperation[] claimOperation,
ref AUTHZ_SECURITY_ATTRIBUTES_INFORMATION claims);
}*/
}
}