static async Task OnUserInformationReceived(UserInformationReceivedContext context)
        {
            Debug.WriteLine($"6. Begin {nameof(OnUserInformationReceived)}");
            await onUserInformationReceived(context);

            Debug.WriteLine($"6. End - {nameof(OnUserInformationReceived)}");
        }
コード例 #2
0
        private async Task OnUserInformationReceivedAsync(UserInformationReceivedContext context)
        {
            _logger.LogDebug(string.Format(CultureInfo.InvariantCulture, LogMessages.MethodBegin, nameof(OnUserInformationReceivedAsync)));
            await s_onUserInformationReceived(context).ConfigureAwait(false);

            _logger.LogDebug(string.Format(CultureInfo.InvariantCulture, LogMessages.MethodEnd, nameof(OnUserInformationReceivedAsync)));
        }
コード例 #3
0
        private static void MapKeyCloakRolesToRoleClaims(UserInformationReceivedContext context)
        {
            if (context.Principal.Identity is not ClaimsIdentity claimsIdentity)
            {
                return;
            }

            if (context.User.RootElement.TryGetProperty("preferred_username", out var username))
            {
                claimsIdentity.AddClaim(new Claim(ClaimTypes.Name, username.ToString()));
            }

            if (context.User.RootElement.TryGetProperty("realm_access", out var realmAccess) &&
                realmAccess.TryGetProperty("roles", out var globalRoles))
            {
                foreach (var role in globalRoles.EnumerateArray())
                {
                    claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, role.ToString()));
                }
            }

            if (context.User.RootElement.TryGetProperty("resource_access", out var clientAccess) &&
                clientAccess.TryGetProperty(context.Options.ClientId, out var client) &&
                client.TryGetProperty("roles", out var clientRoles))
            {
                foreach (var role in clientRoles.EnumerateArray())
                {
                    claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, role.ToString()));
                }
            }
        }
コード例 #4
0
        private async Task OnUserInformationReceivedAsync(UserInformationReceivedContext context)
        {
            _logger.LogDebug($"6. Begin {nameof(OnUserInformationReceivedAsync)}");
            await s_onUserInformationReceived(context).ConfigureAwait(false);

            _logger.LogDebug($"6. End - {nameof(OnUserInformationReceivedAsync)}");
        }
コード例 #5
0
        private static async Task OnUserInformationReceivedAsync(UserInformationReceivedContext context)
        {
            Debug.WriteLine($"6. Begin {nameof(OnUserInformationReceivedAsync)}");
            await s_onUserInformationReceived(context).ConfigureAwait(false);

            Debug.WriteLine($"6. End - {nameof(OnUserInformationReceivedAsync)}");
        }
コード例 #6
0
ファイル: Startup.cs プロジェクト: ajaxe/host-mgmt
        private async Task UserInformationReceived(UserInformationReceivedContext context)
        {
            var userId = await context.HttpContext.RequestServices
                         .GetRequiredService <IUserService>()
                         .AddOrUpdateUserOnLoginAsync(context.Principal.Claims);

            ((ClaimsIdentity)context.Principal.Identity).AddClaim(new Claim(AppClaimTypes.AppUserId, userId.ToString()));
        }
コード例 #7
0
 // Custom claims in the ID token or userinfo endpoint aren't automatically
 // copied into the ClaimsPrincipal, so it must be done manually here.
 // (This method is used by both OpenIdConnectMiddleware definitions in Events.OnUserInformationReceived)
 private Task CopyCustomClaims(UserInformationReceivedContext context)
 {
     if (context.User.TryGetValue("rewardsNumber", out var rewardsNumber))
     {
         var claimsIdentity = (ClaimsIdentity)context.Principal.Identity;
         claimsIdentity.AddClaim(new Claim("rewardsNumber", rewardsNumber?.ToString()));
     }
     return(Task.CompletedTask);
 }
コード例 #8
0
        public override async Task UserInformationReceived(UserInformationReceivedContext context)
        {
            await base.UserInformationReceived(context);

            // AAD fixup
            //if (context.User["name"] is JArray values)
            //{
            //    context.User["name"] = values[0];
            //}
        }
コード例 #9
0
ファイル: Startup.cs プロジェクト: RIDICS/ITJakub
        private string GetUserValueByClaim(UserInformationReceivedContext context, string claimType)
        {
            var claimAction = context.Options.ClaimActions.First(x => x.ClaimType == claimType) as JsonKeyClaimAction;

            if (claimAction == null)
            {
                return(null);
            }

            var value = context.User.Value <string>(claimAction.JsonKey);

            return(value);
        }
コード例 #10
0
        // Not used, but interesting.
        // Source: https://stackoverflow.com/questions/46038509/unable-to-retrieve-claims-in-net-core-2-0
        private Task OnUserInformationReceivedHandler(UserInformationReceivedContext context)
        {
            if (!(context.Principal.Identity is ClaimsIdentity claimsId))
            {
                throw new Exception();
            }

            // Get a list of all claims attached to the UserInformationRecieved context
            var ctxClaims = context.User.Children().ToList();

            foreach (var ctxClaim in ctxClaims)
            {
                var claimType = ctxClaim.Path;
                var token     = ctxClaim.FirstOrDefault();
                if (token == null)
                {
                    continue;
                }

                var claims = new List <Claim>();
                if (token.Children().Any())
                {
                    claims.AddRange(
                        token.Children()
                        .Select(c => new Claim(claimType, c.Value <string>())));
                }
                else
                {
                    claims.Add(new Claim(claimType, token.Value <string>()));
                }

                foreach (var claim in claims)
                {
                    if (!claimsId.Claims.Any(
                            c => c.Type == claim.Type &&
                            c.Value == claim.Value))
                    {
                        claimsId.AddClaim(claim);
                    }
                }
            }

            return(Task.CompletedTask);
        }
コード例 #11
0
        public static async Task OnUserInformationReceived(UserInformationReceivedContext context)
        {
            var userService   = context.HttpContext.RequestServices.GetService <IUserService>();
            var linkGenerator = context.HttpContext.RequestServices.GetService <LinkGenerator>();
            var identity      = (ClaimsIdentity)context.Principal.Identity;
            var user          = await userService.GetUserFromIssuerAsync(context.Principal.FindFirst("iss").Value, context.Principal.FindFirst("sub").Value, context.HttpContext.RequestAborted);

            // Create a new user if they do not exist locally fill and in the defaults.
            if (user == null)
            {
                user = new User
                {
                    Name  = context.User.Value <string>("name"),
                    Email = context.User.Value <string>("email"),
                };
                user.LinkedAccounts = new List <LinkedAccount>
                {
                    new LinkedAccount
                    {
                        Issuer  = context.Principal.FindFirst("iss").Value,
                        Subject = context.Principal.FindFirst("sub").Value,
                        User    = user,
                    }
                };

                await userService.CreateUserAsync(user);

                // Give new uers a chance to update their details
                // 1. Save redirect URL to session
                context.HttpContext.Session.Set(AccountController.RedirectAfterDetailsSessionKey, context.Properties.RedirectUri);
                // 2. Show firtst time page asking for name, details etc,
                context.Properties.RedirectUri = linkGenerator.GetUriByAction(context.HttpContext, nameof(AccountController.Index), "Account");
                // 3. if skipped or accepted, redirect to initial redirect page (See AccountController.UpdateAccount)
            }

            identity.AddClaim(new Claim("user", user.Id.ToString()));
            identity.AddClaim(new Claim("admin", user.IsAdmin.ToString()));
        }
コード例 #12
0
        // Used to set proper user claims
        private static Task SetUserInformationReceived(UserInformationReceivedContext context)
        {
            var claims = new List <Claim>();

            foreach (var jToken in context.User.Children())
            {
                if (context.User.TryGetValue(jToken.Path, value: out var roles))
                {
                    if (roles.Type != JTokenType.Array)
                    {
                        claims.Add(new Claim(roles.Path, roles.ToString()));
                    }

                    else
                    {
                        claims.AddRange(jToken.Select(role => new Claim(role.Path, role.ToString())));
                    }
                }
            }

            (context.Principal.Identity as ClaimsIdentity)?.AddClaims(claims);

            return(Task.CompletedTask);
        }
コード例 #13
0
 private static Task OnUserInformationReceived(UserInformationReceivedContext arg)
 {
     Console.WriteLine(arg);
     return(Task.FromResult(0));
 }
コード例 #14
0
 private static Task OnUserInformationReceived(UserInformationReceivedContext context)
 {
     return(Task.FromResult(0));
 }
コード例 #15
0
 private Task UserInformationReceived(UserInformationReceivedContext context)
 {
     _logger.LogDebug("UserInformationReceived!");
     return(Task.FromResult(0));
 }
コード例 #16
0
 /// <summary>
 /// Invoked when user information is retrieved from the UserInfoEndpoint.
 /// </summary>
 public virtual Task UserInformationReceived(UserInformationReceivedContext context) => OnUserInformationReceived(context);
コード例 #17
0
        public Task UserInformationReceived(UserInformationReceivedContext context)
        {
            _logger.LogInformation("User Info Received", context.ProtocolMessage);

            return(Task.FromResult(0));
        }
コード例 #18
0
 public override Task UserInformationReceived(UserInformationReceivedContext context)
 {
     return(base.UserInformationReceived(context));
 }
コード例 #19
0
 public override Task UserInformationReceived(UserInformationReceivedContext context)
 {
     GetLogger(context).LogCallerMethodName();
     return(base.UserInformationReceived(context));
 }