Esempio n. 1
0
        public async Task <ActionResult> AddUser(UserViewModel userViewModel)
        {
            var cookies = this.HttpContext.Request.Cookies;

            var retroId = Guid.Parse(userViewModel.RetroId);

            var userId = Guid.NewGuid();

            using (var context = new SpeedyRetroDbContext())
            {
                var retrospective = context.Retrospectives.Where(retro => retro.Guid == retroId).SingleOrDefault();

                if (retrospective == null)
                {
                    return(RedirectToRoute("Add-Retro-Route"));
                }

                var userModel = new User
                {
                    Guid           = userId,
                    Name           = userViewModel.Name,
                    Retrospectives = new List <Retrospective>()
                };

                userModel.Retrospectives.Add(retrospective);

                context.Users.Add(userModel);

                await context.SaveChangesAsync();
            }

            var header = new Dictionary <string, object>
            {
                ["alg"] = "HS256",
                ["typ"] = "JWT"
            };

            var payload = new Dictionary <string, object>
            {
                ["iss"]    = "SpeedyRetro",
                ["exp"]    = DateTime.UtcNow.AddYears(1).Second.ToString(),
                ["sub"]    = "UserManagement",
                ["sr_uid"] = userId
            };

            var secret = "SpeedyRetro is great";

            var jwtToken = new JwtToken(header, payload, secret);

            var httpCookie = new HttpCookie("sr_user", jwtToken.ComputedValue());

            httpCookie.Expires = DateTime.UtcNow.AddYears(1);

            this.HttpContext.Response.AppendCookie(httpCookie);

            return(Json(new { }, JsonRequestBehavior.AllowGet));
        }
Esempio n. 2
0
        public async Task <JsonResult> AddRetro(string name)
        {
            var retroId = Guid.NewGuid();

            using (var context = new SpeedyRetroDbContext())
            {
                var defaultPool = context.Pools
                                  .Include("Lanes")
                                  .Where(p => p.Id == 1)
                                  .Single();

                var pool = new Pool
                {
                    Lanes = defaultPool.Lanes,
                    Name  = "New Pool"
                };

                var board = new Board
                {
                    Name   = "New Board",
                    Pool   = pool,
                    PoolId = 1
                };

                var retrospective = new Retrospective
                {
                    Board = board,
                    Guid  = retroId,
                    Name  = name
                };

                context.Retrospectives.Add(retrospective);

                context.SaveChanges();

                board.RetrospectiveId = retrospective.Id;

                pool.BoardId = board.Id;

                await context.SaveChangesAsync();
            }

            return(Json(new { id = retroId }, JsonRequestBehavior.AllowGet));
        }