Example #1
0
        public async void OnGet()
        {
            if (User.Identity.IsAuthenticated)
            {
                OpenIdConnectSessionDetails = HttpContext.Session.Get <OpenIdConnectSessionDetails>(Wellknown.OIDCSessionKey);

                Claims = Request.HttpContext.User.Claims.ToList();
            }
        }
        public void OnGet()
        {
            if (User.Identity.IsAuthenticated)
            {
                var key = this.GetJsonCookie <string>(".oidc.memoryCacheKey");

                var oidcMessage = _cache.Get <OpenIdConnectMessage>(key);

                OpenIdConnectSessionDetails = HttpContext.Session.Get <OpenIdConnectSessionDetails>(Wellknown.OIDCSessionKey);

                Claims = Request.HttpContext.User.Claims.ToList();
            }
        }
Example #3
0
        public override async Task <ClaimsPrincipal> CreateAsync(TUser user)
        {
            var principal = await base.CreateAsync(user);

            var items = _httpContextAccessor.HttpContext.Items;
            OpenIdConnectSessionDetails oidc = items[Wellknown.OIDCSessionKey] as OpenIdConnectSessionDetails;
            bool addIdToken = false;

            if (oidc == null)
            {
                // maybe its in a cookie
                oidc = _httpContextAccessor.HttpContext.Request.Cookies.Get <OpenIdConnectSessionDetails>(
                    Wellknown.OIDCSessionKey);
                _httpContextAccessor.HttpContext.Response.Cookies.Remove(Wellknown.OIDCSessionKey);
                // This is a special case.  as this user just got created and the call to await _signInManager.UpdateExternalAuthenticationTokensAsync(info);
                // isn't helping us to store away the tokens.
                addIdToken = true;
            }

            if (oidc != null)
            {
                ((ClaimsIdentity)principal.Identity).AddClaim(new Claim("login_provider", oidc.LoginProider));
                if (addIdToken)
                {
                    ((ClaimsIdentity)principal.Identity).AddClaim(new Claim("id_token", oidc.OIDC["id_token"]));
                }
            }

            /*
             * get more claims.
             * */
            /*
             * var claims = await _postAuthClaimsProvider.FetchClaims(principal);
             * if (claims != null)
             * {
             *  ((ClaimsIdentity)principal.Identity).AddClaims(claims);
             * }
             */
            return(principal);
        }
Example #4
0
        public async Task <OpenIdConnectSessionDetails> GetOpenIdConnectSessionDetailsAsync()
        {
            OpenIdConnectSessionDetails result = HttpContext.Session.Get <OpenIdConnectSessionDetails>(Wellknown.OIDCSessionKey);

            return(result);
        }
Example #5
0
        public async Task <IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (remoteError != null)
            {
                ErrorMessage = $"Error from external provider: {remoteError}";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }
            var oidc = await HarvestOidcDataAsync();

            // this is the session alternative to storing tokens
            HttpContext.Session.Set(Wellknown.OIDCSessionKey, new OpenIdConnectSessionDetails
            {
                LoginProider = info.LoginProvider,
                OIDC         = oidc
            });

            // here we add the OIDC Login details into the item.
            // This will later be picked up by AppClaimsPrincipalFactory which will add the loginProvider as a claim.
            HttpContext.Items[Wellknown.OIDCSessionKey] = new OpenIdConnectSessionDetails
            {
                LoginProider = info.LoginProvider
            };

            // Sign in the user with this external login provider if the user already has a login.
            var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            if (result.Succeeded)
            {
                // Update the token
                await _signInManager.UpdateExternalAuthenticationTokensAsync(info);

                _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider);
                return(LocalRedirect(returnUrl));
            }
            if (result.IsLockedOut)
            {
                return(RedirectToPage("./Lockout"));
            }
            else
            {
                var openIdConnectSessionDetails = new OpenIdConnectSessionDetails
                {
                    LoginProider = info.LoginProvider,
                    OIDC         = oidc
                };

                HttpContext.Response.Cookies.Set(Wellknown.OIDCSessionKey, openIdConnectSessionDetails, 5);

                // If the user does not have an account, then ask the user to create an account.
                ReturnUrl     = returnUrl;
                LoginProvider = info.LoginProvider;
                if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email))
                {
                    Input = new InputModel
                    {
                        Email = info.Principal.FindFirstValue(ClaimTypes.Email)
                    };
                }
                return(Page());
            }
        }