PageRenderTime 51ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/Archive/2.0/Pegasus.Library/Pegasus.Library/DirectoryServices/Username.cs

#
C# | 98 lines | 59 code | 7 blank | 32 comment | 1 complexity | 41b5415461c2c312ecf04807e55f64fe MD5 | raw file
Possible License(s): AGPL-1.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Pegasus.DirectoryServices
  5. {
  6. /// <summary>
  7. /// Class is used to change and inspect a windows username.
  8. /// </summary>
  9. /// <remarks>
  10. /// A username can be in one of the standard formats
  11. /// bnelson
  12. /// PEGASUS\bnelson
  13. /// </remarks>
  14. public class Username
  15. {
  16. // Local Instance Values;
  17. private string m_domain = string.Empty;
  18. private string m_user = string.Empty;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="T:Username"/> class.
  21. /// </summary>
  22. /// <param name="username">The username.</param>
  23. public Username( string username )
  24. {
  25. m_domain = GetDomain( username );
  26. m_user = GetUsername( username );
  27. }
  28. /// <summary>
  29. /// Gets the domain.
  30. /// </summary>
  31. /// <value>The domain.</value>
  32. public string Domain
  33. {
  34. get
  35. {
  36. return m_domain;
  37. }
  38. }
  39. /// <summary>
  40. /// Gets the user.
  41. /// </summary>
  42. /// <value>The user.</value>
  43. public string User
  44. {
  45. get
  46. {
  47. return m_user;
  48. }
  49. }
  50. /// <summary>
  51. /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
  52. /// </summary>
  53. /// <returns>
  54. /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
  55. /// </returns>
  56. public override string ToString()
  57. {
  58. return string.Format( "{0}\\{1}", m_domain, m_user );
  59. }
  60. /// <summary>
  61. /// Gets the username.
  62. /// </summary>
  63. /// <param name="username">The username.</param>
  64. /// <returns></returns>
  65. public static string GetUsername( string username )
  66. {
  67. int pos = username.IndexOf( "\\" );
  68. if( pos > -1 )
  69. {
  70. return username.Substring( pos + 1 );
  71. }
  72. return username;
  73. }
  74. /// <summary>
  75. /// Gets the domain.
  76. /// </summary>
  77. /// <param name="username">The username.</param>
  78. /// <returns></returns>
  79. public static string GetDomain( string username )
  80. {
  81. int pos = username.IndexOf( "\\" );
  82. if( pos > -1 )
  83. {
  84. return username.Substring( 0, pos );
  85. }
  86. return string.Empty;
  87. }
  88. }
  89. }