public async Task <IActionResult> Register(RegisterRequest req, [FromQuery] string redirect = "") { if (!ModelState.IsValid) { return(View()); } try { ViewData["RedirectTo"] = new PathString(redirect); } catch (ArgumentException) { ViewData["RedirectTo"] = new PathString(""); } var user = await Db.Users.SingleOrDefaultAsync(x => x.Username == req.Username && x.FullName == req.FullName); if (user != null) { ModelState.AddModelError("", "User already exists"); return(View()); } Invite inv = await Db.Invites .SingleAsync(x => x.Uid == req.InviteID); User newUser = new User() { RoleId = inv.RoleId, // Anonymous role Username = req.Username, FullName = req.FullName, PasswordSalt = AuthUtils.GetRandomData(64) }; newUser.PasswordHash = AuthUtils.GetHashFor(req.Password, newUser.PasswordSalt); await Db.AddAsync(newUser); Db.Remove(inv); await Db.SaveChangesAsync(); var userIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme); userIdentity.AddClaims(new Claim[] { new Claim(ClaimTypes.PrimarySid, newUser.Id.ToString()), new Claim(ClaimTypes.NameIdentifier, newUser.Username), new Claim(ClaimTypes.Name, newUser.FullName), new Claim(ClaimTypes.Role, newUser.RoleId.ToString()) }); var principal = new ClaimsPrincipal(userIdentity); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal); try { return(Redirect(new PathString(redirect))); } catch (ArgumentException) { return(RedirectToAction("News", "Issue")); } }
public async Task <string> StoreAsync(AuthenticationTicket ticket) { using (var ctx = new PBugContext(dbOptions)) { Session s = (await ctx.AddAsync(new Session() { Id = AuthUtils.GetRandomData(64), SessionData = TicketSerializer.Default.Serialize(ticket), Expires = ticket.Properties.ExpiresUtc })).Entity; await ctx.SaveChangesAsync(); return(Convert.ToBase64String(s.Id)); } }
public async Task <IActionResult> Create(CreateIssueRequest req) { if (!ModelState.IsValid) { return(View()); } if (HttpContext.Request.Form.Files.Sum(x => x.Length) >= 128 * 1024 * 1024) { ModelState.AddModelError("", "File size too large."); return(View()); } Issue i = (await Db.Issues.AddAsync(new Issue() { Name = req.Name, Tags = req.Tags ?? "", Description = req.Description, ProjectId = req.ProjectID, AssigneeId = req.AssigneeID == -1 ? null : new uint?((uint)req.AssigneeID), AuthorId = HttpContext.User.IsAnonymous() ? null : new uint?((uint)HttpContext.User.GetUserId()), DateOfCreation = DateTime.UtcNow, Status = IssueStatus.Open })).Entity; if (!HttpContext.User.IsAnonymous()) { await Db.AddAsync(new IssueWatcher() { Issue = i, WatcherId = (uint)HttpContext.User.GetUserId() }); } await Db.IssueActivities.AddIssueActivity(HttpContext, 0, new CreateIssueActivity() { Issue = i, Name = req.Name, Description = req.Description, Tags = req.Tags ?? "", ProjectId = req.ProjectID, AssigneeId = req.AssigneeID == -1 ? null : new uint?((uint)req.AssigneeID), }); List <(IssueFile, IFormFile)> toProcess = new List <(IssueFile, IFormFile)>(); foreach (IFormFile file in HttpContext.Request.Form.Files) { toProcess.Add(( (await Db.IssueFiles.AddAsync(new IssueFile() { Issue = i, FileName = file.FileName, FileId = Convert.ToBase64String(AuthUtils.GetRandomData(12)) .Replace('/', '_') .Replace('+', '-') })).Entity, file)); } await Db.SaveChangesAsync(); foreach ((IssueFile, IFormFile)pack in toProcess) { using (FileStream fs = System.IO.File.OpenWrite(Path.Combine("files", pack.Item1.FileId))) await pack.Item2.CopyToAsync(fs); } return(RedirectToAction("ViewTalk", new { id = i.Id })); }