public static AppUser GetUser(string username) { DbConnection cnn = new SqlConnection(Application.HRConnectionString); AppUser user = null; #if !DEBUG try { if (Current.Profiler != null) cnn = new StackExchange.Profiling.Data.ProfiledDbConnection(cnn, new ErrorLoggingProfiler(Current.Profiler)); cnn.Open(); user = cnn.Query<User>("select * from Users where Username=@Username", new { username }).FirstOrDefault(); } finally { if (cnn != null) { cnn.Dispose(); } } #else user = new AppUser() { Username = username, FullName = "พิฐากูร สุวรรณเนกข์", Department = "ศรัทธาภิบาล", Section = "ธรรมวารี", MemberType = "อาสาสมัคร" }; #endif return user; }
public static AppUserViewModel FromModel(AppUser model) { return new AppUserViewModel() { Id = model.Id.ToString(), Username = model.Username, UserType = ((int)model.UserType).ToString(), Email = model.Email }; }
public static AppUser ToModel(AppUserViewModel viewModel) { var user = new AppUser() { Id = viewModel.Id.IsNullOrEmptyReturn<int>(), Username = viewModel.Username, UserType = (UserType)int.Parse(viewModel.UserType), Email = viewModel.Email ?? "" }; return user; }
public static void Update(AppUser model, out bool foundDup) { foundDup = false; var updateModel = Current.DB.Users.Get(model.Id); var snapshot = Snapshotter.Start(updateModel); updateModel.Id = model.Id; updateModel.Username = model.Username; updateModel.UserType = model.UserType; updateModel.Email = model.Email; var diff = snapshot.Diff(); if (diff.ParameterNames.Any()) { try { Current.DB.Users.Update(updateModel.Id, diff); } catch (SqlException ex) { switch (ex.Number) { case 2627: // 2627 is unique constraint (includes primary key) case 2601: // 2601 is unique index foundDup = true; break; default: throw; } } } }
public static AppUser CreateLoginUser(AppUser user) { user.UserType = UserType.Customer; // ใช้ insert อย่างเดียว user.CreationDate = DateTime.Now; user = Current.DB.Query<AppUser>(@" begin tran update Users with (serializable) set FullName = @FullName, MemberType = @MemberType, Department = @Department, Section = @Section where Username = @Username if @@rowcount = 0 begin insert Users (Username,UserType,FullName,MemberType,Department,Section,CreationDate) values (@Username,@UserType,@FullName,@MemberType,@Department,@Section,@CreationDate) end select * FROM Users WHERE Username = @Username commit tran", user).Single(); return user; }
public static void Create(AppUser model, out bool foundDup) { foundDup = false; try { model.CreationDate = DateTime.Now; model.Id = (int)Current.DB.Users.Insert( new { model.Username, model.UserType, model.Email, model.CreationDate, }); } catch (SqlException ex) { switch (ex.Number) { case 2627: // 2627 is unique constraint (includes primary key) case 2601: // 2601 is unique index foundDup = true; break; default: throw; } } }
private static AppUser GetCurrentUser(bool isAuthenticated, string userHostAddress, string identity) { var user = new AppUser(); if (isAuthenticated) { int id; if (Int32.TryParse(identity, out id)) { AppUser lookup = Current.DB.Users.Get(id); if (lookup != null) { user = lookup; } } else { FormsAuthentication.SignOut(); } } //user.IPAddress = userHostAddress; return user; }
/// <summary> /// initializes current user based on the current Request's cookies/authentication status. This /// method could return a newly created, Anonymous User if no means of identification are found. /// </summary> protected void InitCurrentUser() { _currentUser = GetCurrentUser(Request, User.Identity.Name); }
private void CreateAuthenCookie(AppUser user) { DateTime ticketTimeout = DateTime.Now.AddMinutes(Application.TicketTimeout); bool isPersistent = false; var ticket = new FormsAuthenticationTicket( 1, user.Id.ToString(), DateTime.Now, ticketTimeout, // timeout: This means that after a certain amount of time of inactivity, a user is prompted to login again. isPersistent, user.UserType.ToString()); string encryptedTicket = FormsAuthentication.Encrypt(ticket); var authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) { HttpOnly = true, Secure = FormsAuthentication.RequireSSL, Path = FormsAuthentication.FormsCookiePath, Domain = FormsAuthentication.CookieDomain }; if (isPersistent) { authenticationCookie.Expires = ticket.Expiration; // If not set an expiry date it defaults to expire at the end of the session } Response.AppendCookie(authenticationCookie); }