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\


 

Comments (5)

  1. Themes says:

    Hmm, interesting!

    As far as I know, Danny Thorpe is one of Borland developers. Why he is placing annotations into this book?

    Is he now working inside Microsoft or may be he is external co-author expert of your book?

  2. BradA says:

    Danny is still very much at Borland.. he worked on the CLI specification as part of the ECMA working group. It is really good to have 3rd party involvement in making the standard and the product.

  3. Mark Constantine says:

    This is a step in the right direction. To go all the way, stop passing -any- kind of control flag into methods. We’ve known that "control coupling" is bad ever since the structured programming days. To get past it in the example above, create subclasses for each case in your enumeration. Then you don’t need the enumeration any more.

    For more information on why control coupling is bad, see any old book on structured design from the 1970s, or Bertrand Meyer’s Object Oriented Software Construction. There are many more useful design guidelines in the latter reference.