Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Authentication

Public general use code classes and xml files that we've compiled and used over the years:

Manage and verify user logging and passwords. The administrator will define the user's password and logging website. The service will issue a true of false according to authentication.

   1:  using System;
   2:  using System.Web;
   3:  using System.Xml;
   4:  using System.Xml.Linq;
   5:  using System.Linq;
   6:  using System.Text.RegularExpressions;
   7:  using System.Collections;
   8:  using System.Collections.Generic;
   9:   
  10:  namespace Ia.Cl.Model
  11:  {
  12:      ////////////////////////////////////////////////////////////////////////////
  13:   
  14:      /// <summary publish="true">
  15:      /// Manage and verify user logging and passwords. The administrator will define the user's password and logging website. The service will issue a true of false according to authentication.
  16:      /// </summary>
  17:      /// 
  18:      /// <code>
  19:      /// bool b;
  20:      /// Uri uri = new Uri("http://*");
  21:      /// b = Ia.Cl.Model.Authentication.Validate("name", uri);
  22:      /// </code>
  23:      /// 
  24:      /// </summary>
  25:      /// <remarks> 
  26:      /// Copyright � 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  27:      ///
  28:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  29:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  30:      ///
  31:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  32:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  33:      /// 
  34:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  35:      /// 
  36:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  37:      /// </remarks>
  38:   
  39:      ////////////////////////////////////////////////////////////////////////////
  40:   
  41:      public partial class Authentication
  42:      {
  43:          /*
  44:          static int seed;
  45:          static Random r;
  46:  
  47:          static string number = "1234567890";
  48:          static string uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  49:          static string lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
  50:  
  51:          private const string traceCookieName = "traceCookie";
  52:           */
  53:   
  54:          /// <summary/>
  55:          public Authentication() { }
  56:   
  57:          /// <summary/>
  58:          public int Id { get; set; }
  59:          /// <summary/>
  60:          public string Password { get; set; }
  61:          /// <summary/>
  62:          public string Host { get; set; }
  63:          /// <summary/>
  64:          public DateTime Created { get; set; }
  65:          /// <summary/>
  66:          public DateTime Updated { get; set; }
  67:   
  68:          ////////////////////////////////////////////////////////////////////////////
  69:   
  70:          /// <summary>
  71:          ///
  72:          /// </summary>
  73:          public static bool Create(Authentication newItem, out string result)
  74:          {
  75:              bool b;
  76:   
  77:              b = false;
  78:              result = "";
  79:   
  80:              using (var db = new global::Ia.Cl.Model.Ia())
  81:              {
  82:                  db.Authentications.Add(newItem);
  83:                  db.SaveChanges();
  84:   
  85:                  b = true;
  86:              }
  87:   
  88:              return b;
  89:          }
  90:   
  91:          ////////////////////////////////////////////////////////////////////////////
  92:   
  93:          /// <summary>
  94:          ///
  95:          /// </summary>
  96:          public static void Create(string password, Uri url, Guid guid)
  97:          {
  98:              bool newItemCreated;
  99:              string result;
 100:              Authentication newItem;
 101:   
 102:              newItem = new Authentication();
 103:   
 104:              // insert new item
 105:              newItem.Password = password;
 106:              newItem.Host = global::Ia.Cl.Model.Default.BasicHost(url);
 107:              newItem.Created = DateTime.UtcNow.AddHours(3);
 108:   
 109:              newItemCreated = Authentication.Create(newItem, out result);
 110:          }
 111:   
 112:          ////////////////////////////////////////////////////////////////////////////
 113:   
 114:          /// <summary>
 115:          /// Validate a password by sending it and the Uri of the website to this function
 116:          /// <param name="password">Password of the user</param>
 117:          /// <param name="uri">Uri of webpage</param>
 118:          /// </summary>
 119:          public static bool Validate(string password, Uri uri)
 120:          {
 121:              bool validated;
 122:              string host;
 123:              Authentication item;
 124:   
 125:              host = global::Ia.Cl.Model.Default.BasicHost(uri);
 126:   
 127:              using (var db = new global::Ia.Cl.Model.Ia())
 128:              {
 129:                  // the matching is case-insensitive
 130:                  item = (from q in db.Authentications where q.Password.ToLower() == password.ToLower() && q.Host == host select q).SingleOrDefault();
 131:   
 132:                  validated = (item != null);
 133:              }
 134:   
 135:              return validated;
 136:          }
 137:   
 138:          ////////////////////////////////////////////////////////////////////////////
 139:   
 140:          /// <summary>
 141:          ///
 142:          /// </summary>
 143:          public static Authentication Read(int id)
 144:          {
 145:              Authentication item;
 146:   
 147:              using (var db = new global::Ia.Cl.Model.Ia())
 148:              {
 149:                  item = (from q in db.Authentications where q.Id == id select q).SingleOrDefault();
 150:              }
 151:   
 152:              return item;
 153:          }
 154:   
 155:          ////////////////////////////////////////////////////////////////////////////
 156:   
 157:          /// <summary>
 158:          ///
 159:          /// </summary>
 160:          public static List<Authentication> ReadList()
 161:          {
 162:              List<Authentication> list;
 163:   
 164:              using (var db = new global::Ia.Cl.Model.Ia())
 165:              {
 166:                  list = (from q in db.Authentications select q).ToList();
 167:              }
 168:   
 169:              return list;
 170:          }
 171:   
 172:          ////////////////////////////////////////////////////////////////////////////
 173:   
 174:          /// <summary>
 175:          ///
 176:          /// </summary>
 177:          public static bool Update(Authentication updatedItem, out string result)
 178:          {
 179:              bool b;
 180:   
 181:              b = false;
 182:              result = "";
 183:   
 184:              using (var db = new global::Ia.Cl.Model.Ia())
 185:              {
 186:                  updatedItem = (from q in db.Authentications where q.Id == updatedItem.Id select q).SingleOrDefault();
 187:   
 188:                  updatedItem.Updated = DateTime.UtcNow.AddHours(3);
 189:   
 190:                  db.Authentications.Attach(updatedItem);
 191:   
 192:                  var v = db.Entry(updatedItem);
 193:                  v.State = System.Data.Entity.EntityState.Modified;
 194:                  db.SaveChanges();
 195:   
 196:                  b = true;
 197:              }
 198:   
 199:              return b;
 200:          }
 201:   
 202:          ////////////////////////////////////////////////////////////////////////////
 203:   
 204:          /// <summary>
 205:          ///
 206:          /// </summary>
 207:          public static bool Delete(int id, out string result)
 208:          {
 209:              bool b;
 210:   
 211:              b = false;
 212:              result = "";
 213:   
 214:              using (var db = new global::Ia.Cl.Model.Ia())
 215:              {
 216:                  var v = (from q in db.Authentications where q.Id == id select q).FirstOrDefault();
 217:   
 218:                  db.Authentications.Remove(v);
 219:                  db.SaveChanges();
 220:   
 221:                  b = true;
 222:              }
 223:   
 224:              return b;
 225:          }
 226:   
 227:          ////////////////////////////////////////////////////////////////////////////
 228:   
 229:          /// <summary>
 230:          /// Returns randomly generated strings in password and authenticate sting formats for copy and past purposes.
 231:          /// The format is variable according to user preference.
 232:          /// </summary>
 233:          public static string RandomPasswordOfLength(int passwordLength)
 234:          {
 235:              string password;
 236:   
 237:              password = Guid.NewGuid().ToString().ToLower().Substring(0, passwordLength);
 238:   
 239:              return password;
 240:          }
 241:   
 242:          /*
 243:          ////////////////////////////////////////////////////////////////////////////
 244:  
 245:          /// <summary>
 246:          ///
 247:          /// </summary>
 248:          public static string ReturnStringWithNumberAndUpperAndLowerLetters(int length)
 249:          {
 250:              int i;
 251:              string range, s;
 252:  
 253:              // for a 64 long string
 254:              range = number + uppercaseLetters + lowercaseLetters; s = "";
 255:              for (i = 0; i < length; i++) s += RandomCharacter(range.ToCharArray(), r);
 256:  
 257:              return s;
 258:          }
 259:  
 260:          ////////////////////////////////////////////////////////////////////////////
 261:  
 262:          /// <summary>
 263:          ///
 264:          /// </summary>
 265:          public static string ReturnStringWithNumberAndUpperLetters(int length)
 266:          {
 267:              int i;
 268:              string range, s;
 269:  
 270:              range = number + uppercaseLetters; s = "";
 271:  
 272:              for (i = 0; i < length; i++)
 273:              {
 274:                  s += RandomCharacter(range.ToCharArray(), r);
 275:                  if ((i + 1) % 5 == 0 && (i + 1) < 30) s += "-";
 276:              }
 277:  
 278:              return s;
 279:          }
 280:  
 281:          ////////////////////////////////////////////////////////////////////////////
 282:  
 283:          /// <summary>
 284:          ///
 285:          /// </summary>
 286:          public static string ReturnStringWithNumberAndLowerLetters(int length)
 287:          {
 288:              int i;
 289:              string range, s;
 290:  
 291:              range = number + lowercaseLetters; s = "";
 292:  
 293:              for (i = 0; i < length; i++) s += RandomCharacter(range.ToCharArray(), r);
 294:  
 295:              return s;
 296:          }
 297:  
 298:          ////////////////////////////////////////////////////////////////////////////
 299:  
 300:          /// <summary>
 301:          ///
 302:          /// </summary>
 303:          private static char RandomCharacter(char[] line, Random r)
 304:          {
 305:              // return a random char from line
 306:              int i;
 307:              i = r.Next(line.Length);
 308:              return line[i];
 309:          }
 310:           */
 311:   
 312:          ////////////////////////////////////////////////////////////////////////////
 313:          ////////////////////////////////////////////////////////////////////////////
 314:      }
 315:  }