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 Ia.Cl.Model.IaDbContext())
76: {
77: //db.Authentications.Add(newItem);
78: //db.SaveChanges();
79:
80: b = false; // 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 = 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 = Ia.Cl.Model.Default.BasicHost(uri);
121:
122: using (var db = new Ia.Cl.Model.IaDbContext())
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 = false; // (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 Ia.Cl.Model.IaDbContext())
143: {
144: item = null; // (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 Ia.Cl.Model.IaDbContext())
160: {
161: list = null; // (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 Ia.Cl.Model.IaDbContext())
180: {
181: /*
182: updatedItem = (from q in db.Authentications where q.Id == updatedItem.Id select q).SingleOrDefault();
183:
184: updatedItem.Updated = DateTime.UtcNow.AddHours(3);
185:
186: db.Authentications.Attach(updatedItem);
187:
188: var v = db.Entry(updatedItem);
189: v.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
190: db.SaveChanges();
191: */
192:
193: b = false; // true;
194: }
195:
196: return b;
197: }
198:
199: ////////////////////////////////////////////////////////////////////////////
200:
201: /// <summary>
202: ///
203: /// </summary>
204: public static bool Delete(int id, out string result)
205: {
206: bool b;
207:
208: b = false;
209: result = "";
210:
211: using (var db = new Ia.Cl.Model.IaDbContext())
212: {
213: //var v = (from q in db.Authentications where q.Id == id select q).FirstOrDefault();
214:
215: //db.Authentications.Remove(v);
216: //db.SaveChanges();
217:
218: b = false; // true;
219: }
220:
221: return b;
222: }
223:
224: ////////////////////////////////////////////////////////////////////////////
225:
226: /// <summary>
227: /// Returns randomly generated strings in password and authenticate sting formats for copy and past purposes.
228: /// The format is variable according to user preference.
229: /// </summary>
230: public static string RandomPasswordOfLength(int passwordLength)
231: {
232: string password;
233:
234: password = Guid.NewGuid().ToString().ToLower().Substring(0, passwordLength);
235:
236: return password;
237: }
238:
239: /*
240: ////////////////////////////////////////////////////////////////////////////
241:
242: /// <summary>
243: ///
244: /// </summary>
245: public static string ReturnStringWithNumberAndUpperAndLowerLetters(int length)
246: {
247: int i;
248: string range, s;
249:
250: // for a 64 long string
251: range = number + uppercaseLetters + lowercaseLetters; s = "";
252: for (i = 0; i < length; i++) s += RandomCharacter(range.ToCharArray(), r);
253:
254: return s;
255: }
256:
257: ////////////////////////////////////////////////////////////////////////////
258:
259: /// <summary>
260: ///
261: /// </summary>
262: public static string ReturnStringWithNumberAndUpperLetters(int length)
263: {
264: int i;
265: string range, s;
266:
267: range = number + uppercaseLetters; s = "";
268:
269: for (i = 0; i < length; i++)
270: {
271: s += RandomCharacter(range.ToCharArray(), r);
272: if ((i + 1) % 5 == 0 && (i + 1) < 30) s += "-";
273: }
274:
275: return s;
276: }
277:
278: ////////////////////////////////////////////////////////////////////////////
279:
280: /// <summary>
281: ///
282: /// </summary>
283: public static string ReturnStringWithNumberAndLowerLetters(int length)
284: {
285: int i;
286: string range, s;
287:
288: range = number + lowercaseLetters; s = "";
289:
290: for (i = 0; i < length; i++) s += RandomCharacter(range.ToCharArray(), r);
291:
292: return s;
293: }
294:
295: ////////////////////////////////////////////////////////////////////////////
296:
297: /// <summary>
298: ///
299: /// </summary>
300: private static char RandomCharacter(char[] line, Random r)
301: {
302: // return a random char from line
303: int i;
304: i = r.Next(line.Length);
305: return line[i];
306: }
307: */
308:
309: ////////////////////////////////////////////////////////////////////////////
310: ////////////////////////////////////////////////////////////////////////////
311: }
312: }