The SLAR (vol2) on System.Net.Authorization

Continuing in the series sharing some of the information in the .NET Framework Standard Library Annotated Reference Vol 1 and .NET Framework Standard Library Annotated Reference Vol 2 with some information on Authorization.

Danny Thorpe

Avoid creating methods with Boolean parameters. Boolean parameters make calls harder to

read and harder to write. Quick! What’s the difference between Authorization("foo",

true) and Authorization("foo", false)? There’s no telling what those true/false

parameters do on the inside. Adding an enum type AuthorizationCompletion with

Pending and Finished members would allow those constructor calls to read

Authorization("foo", AuthorizationCompletion.Pending) and

Authorization("foo", AuthorizationCompletion.Finished). At the same time, it

might make sense for the Complete property’s type to be the same enum type as the

constructor parameters, for consistency.

using System;

using System.Net;

using System.Text;

public class AuthorizationSample

{

public static void Main()

{

String credentials = "username:password";

ASCIIEncoding e = new ASCIIEncoding();

Byte[] bytes = e.GetBytes(credentials);

String token = "BASIC " + Convert.ToBase64String(bytes);

Authorization au = new Authorization(token, false, "Group1");

String realm1 = "c:\\samples\\mytestsamples\\";

String realm2 = "c:\\data\\mytestdata\\";

String[] realms = {realm1, realm2};

au.ProtectionRealm = realms;

Console.WriteLine("Authorization Complete = {0}", au.Complete);

Console.WriteLine("Authorization ConnectionGroupId = '{0}'",au.ConnectionGroupId);

Console.WriteLine("Authorization.Message = '{0}'",au.Message);

Console.WriteLine();

Console.WriteLine("Authorization.ProtectionRealm:");

foreach (String s in au.ProtectionRealm)

{

Console.WriteLine(s);

}

}

}

The output is

Authorization Complete = False

Authorization ConnectionGroupId = 'Group1'

Authorization.Message = 'BASIC dXNlcm5hbWU6cGFzc3dvcmQ='

Authorization.ProtectionRealm:

c:\samples\mytestsamples\

c:\data\mytestdata\