public IActionResult TestOIDC()
        {
            var logger = _loggerFactory.CreateLogger("TestOIDC Controller");

            logger.LogInformation("entered testoidc action");
            List <string> messages = new List <string>()
            {
                $"Username: {User.Identity.Name}",
                $"Authenticated: {User.Identity.IsAuthenticated}",
                $"Claims:"
            };

            foreach (var identity in User.Identities)
            {
                messages.AddRange(identity.Claims.Select(c => $"{c.Type}: {c.Value}"));
            }
            //messages.AddRange(User.Claims.Select(c => $"{c.Type}: {c.Value}"));

            OIDCUser user = _dbContext.OIDCUsers.FirstOrDefault(u => u.Subject == User.Claims.First(c => c.Type == "sub").Value);

            if (user != null)
            {
                logger.LogInformation("user found!");
                messages.Add($"Database ID: {user.Id}");
            }
            else
            {
                logger.LogInformation("user not found");
            }
            return(View(messages));
        }
Exemplo n.º 2
0
        public override async Task SignedIn(CookieSignedInContext context)
        {
            var logger = _loggerFactory.CreateLogger("MyCookieAuthenticationEvents");

            logger.LogInformation("Entering SignedIn Event");
            string   name     = context.Principal.Identity.Name;
            Claim    subClaim = context.Principal.Claims.First(c => c.Type == "sub");
            OIDCUser user     = await _dbContext.OIDCUsers.FirstOrDefaultAsync(u => u.Subject == subClaim.Value);

            if (user == null)
            {
                logger.LogInformation("Creating user");
                _dbContext.OIDCUsers.Add(new OIDCUser()
                {
                    Name    = name,
                    Subject = subClaim.Value
                });
                await _dbContext.SaveChangesAsync();
            }
            else
            {
                logger.LogInformation("User already created");
            }
        }