示例#1
0
        public Task InvokeAsync(HttpContext context, CodeEventsDbContext dbContext)
        {
            var authedUser = context.User;

            if (!authedUser.Identity.IsAuthenticated)
            {
                return(_next(context));
            }

            if (authedUser.FindFirstValue("newUser") != "true")
            {
                return(_next(context));
            }

            var user = new User(authedUser);

            var userExists = dbContext.Users.Count(u => u.AzureOId == user.AzureOId) == 1;

            if (userExists)
            {
                return(_next(context));
            }

            dbContext.Users.Add(user);
            dbContext.SaveChanges();

            return(_next(context));
        }
        public Task InvokeAsync(HttpContext context, CodeEventsDbContext dbContext)
        {
            var authedUser = context.User;

            if (!authedUser.Identity.IsAuthenticated || authedUser.FindFirstValue("userId") != null)
            {
                return(_next(context));
            }

            var user   = new User(authedUser);
            var userId = dbContext.Users.First(u => u.AzureOId == user.AzureOId).Id;

            // inject user id into context.User
            authedUser.Identities.FirstOrDefault()
        ? // prevent NPE if no identity is found (unlikely but suppress warning)
            .AddClaim(new Claim("userId", userId.ToString()));

            return(_next(context));
        }
 public CodeEventService(CodeEventsDbContext dbContext)
 {
     _dbContext = dbContext;
 }