شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

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