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