private async Task<User> GetUser(int userId) { using (var context = new DataContext()) { var user = await (from u in context.Users .Include(u => u.Promotions) .Include(u => u.Profile) .Include(u => u.Roles) .Include(u => u.Claims) .Include(u => u.Logins) .Include(u => u.Profile.Federation) .Include(u => u.Profile.Dojo) .Include(u=>u.Profile.Events) where u.Id == userId select u) .FirstOrDefaultAsync(); user.Promotions.Sort(); if (user.DateOfBirth == null) { user.DateOfBirth = (DateTime)SqlDateTime.MinValue; await context.SaveChangesAsync(); } return user; } }
public async Task<IHttpActionResult> PostSearch([FromUri]SearchValues searchValues) { // create a default searchValues, this should probably just be a strut if (searchValues == null) { searchValues = new SearchValues { OrderBy = "Id", Page = 1, PageSize = 20, SortDirection = SortDirection.Ascending }; } int totalItemCount; int skip = (searchValues.Page - 1) * searchValues.PageSize; List<User> userList; // TODO repository, caching etc. using (var context = new DataContext()) { context.Configuration.ProxyCreationEnabled = false; totalItemCount = context.Users.Count(); userList = await (from x in context.Users .Include(u => u.Profile) orderby searchValues.OrderBy select x ).Skip(skip).Take(searchValues.PageSize).ToListAsync(); } var model = new SerializablePagination<User>( userList.ToList(), totalItemCount, searchValues.Page, searchValues.PageSize) { BaseUrl = "User", SortBy = searchValues.OrderBy, SortDirection = searchValues.SortDirection }; return await Task.FromResult((IHttpActionResult)this.Ok(model)); }
private async Task<ICollection<User>> GetUserList(int pagesize, string sortby, int skip, bool onlyShowActive) { using (var context = new DataContext()) { context.Configuration.ProxyCreationEnabled = false; User[] userList; if (onlyShowActive) { userList = await (from x in context.Users where x.Active orderby "Active", sortby select x) .Skip(skip) .Take(pagesize) .ToArrayAsync(); } else { userList = await (from x in context.Users orderby "Active", sortby select x) .Skip(skip) .Take(pagesize) .ToArrayAsync(); } return userList; } }
public async Task<IHttpActionResult> GetForId(int id) { using (var context = new DataContext()) { var user = await (from u in context.Users .Include(u => u.Promotions) .Include(u => u.Profile) .Include(u => u.Roles) .Include(u => u.Claims) .Include(u => u.Logins) .Include(u => u.Profile.Federation) .Include(u => u.Profile.Dojo) .Include(u => u.Profile.Events) where u.Id == id select u) .FirstOrDefaultAsync(); user.Promotions.Sort(); if (user.DateOfBirth == null) { user.DateOfBirth = (DateTime)SqlDateTime.MinValue; await context.SaveChangesAsync(); } return await Task.FromResult((IHttpActionResult)this.Ok(user)); } }
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, AccountLinkingInfoViewModel accountLinkingModel, string returnUrl) { if (this.User.Identity.IsAuthenticated) { return this.RedirectToAction("Index", "Manage"); } if (this.ModelState.IsValid) { // Get the information about the user from the external login provider var info = await this.AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return this.View("ExternalLoginFailure"); } User user; IdentityResult result = null; bool isSuccessful = true; if (accountLinkingModel.AuskfId > 0) { //Find user by auskf id if it is provided using (var context = new DataContext()) { user = (from x in context.Users.Include(u => u.Profile) where x.AuskfId == accountLinkingModel.AuskfId select x).FirstOrDefault(); user.UserName = model.Email; } } else { using (var context = new DataContext()) { int auskfId = (from x in context.Users select x.AuskfId).Max(); // TODO This should not be created here, move to factory user = new User { Profile = new UserProfile(), AuskfId = auskfId }; } } user.UserName = model.Email; user.PasswordLastChangedDate = DateTime.Now; user.MaximumDaysBetweenPasswordChange = 180; user.Email = model.Email; user.JoinedDate = DateTime.Now; user.LastLogin = DateTime.Now; user.Active = true; if (accountLinkingModel.AuskfId == 0) { result = await this.UserManager.CreateAsync(user); isSuccessful = result.Succeeded; } if (isSuccessful) { result = await this.UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await this.SignInManager.SignInAsync(user, false, false); return this.RedirectToLocal(returnUrl); } } this.AddErrors(result); } this.ViewBag.ReturnUrl = returnUrl; return this.View(model); }
public async Task<ActionResult> Register(RegisterViewModel model, AccountLinkingInfoViewModel accountLinkingModel) { try { if (this.ModelState.IsValid) { User user; IdentityResult result; if (accountLinkingModel.AuskfId > 0) { using (var context = new DataContext()) { user = (from x in context.Users.Include(u => u.Profile) where x.AuskfId == accountLinkingModel.AuskfId select x).FirstOrDefault(); } user.UserName = model.Email; user.Email = model.Email; user.Password = model.Password; result = await this.UserManager.UpdateAsync(user); } else { user = new User { UserName = model.Email, Email = model.Email }; result = await this.UserManager.CreateAsync(user, model.Password); } if (result.Succeeded) { var code = await this.UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var url = this.Request.Url; if (url != null) { var callbackUrl = this.Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code }, url.Scheme); await this.UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"); this.ViewBag.Link = callbackUrl; } return this.View("DisplayEmail"); } this.AddErrors(result); } } catch (System.Data.Entity.Validation.DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Value: \"{1}\", Error: \"{2}\"", ve.PropertyName, eve.Entry.CurrentValues.GetValue<object>(ve.PropertyName), ve.ErrorMessage); } } throw; } // If we got this far, something failed, redisplay form return this.View(model); }