Authenticator2.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:2k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. namespace Wrox.ProCSharp.OOProg
  3. {
  4.    class MainEntryPoint
  5.    {
  6.       static void Main()
  7.       {
  8.          Authenticator myAccess = new Authenticator();
  9.          bool done;
  10.          done = myAccess.ChangePassword("", "MyNewPassword");
  11.          if (done == true) 
  12.             Console.WriteLine("Password for myAccess changed");
  13.          else
  14.             Console.WriteLine("Failed to change password for myAccess");
  15.          done = myAccess.ChangePassword("", "AnotherPassword");
  16.          if (done == true) 
  17.             Console.WriteLine("Password for myAccess changed");
  18.          else
  19.             Console.WriteLine("Failed to change password for myAccess");
  20.          if (myAccess.IsPasswordCorrect("WhatPassword"))
  21.             Console.WriteLine("Verified myAccess' password");
  22.          else
  23.             Console.WriteLine("Failed to verify myAccess' password");
  24.       }
  25.    }
  26.    public class Authenticator
  27.    {
  28.       // implementation as shown earlier
  29.       private string password = "";
  30.       private static uint minPasswordLength = 6;
  31.       public static uint GetMinPasswordLength()
  32.       {
  33.          return minPasswordLength;
  34.       }
  35.       public bool IsPasswordCorrect(string tryPassword)
  36.       {
  37.          return (tryPassword == password) ? true : false;
  38.       }
  39.       public bool ChangePassword(string oldPassword, string newPassword)
  40.       {
  41.          if (oldPassword == password)
  42.          {
  43.             password = newPassword;
  44.             return true;
  45.          }
  46.          else
  47.             return false;
  48.       }
  49.    }
  50. }