Exemplo n.º 1
0
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            var   authorizationHeader = Request.Headers.TryGetValue("Authorization", out var v) ? v : default;
            Match m;

            if (authorizationHeader == StringValues.Empty || !(m = Regex.Match(authorizationHeader[0], "Bearer (?<token>.+)")).Success)
            {
                return(AuthenticateResult.NoResult());
            }

            var token = m.Groups["token"].Value;

            PersonProfile?profile = null;

            if (token.Equals(UserToken, StringComparison.InvariantCultureIgnoreCase))
            {
                profile = await _personProjectionStore.GetProfileAsync(TestScheme, UserPersonId.ToString());
            }

            if (profile == null)
            {
                return(AuthenticateResult.NoResult());
            }

            var principal = new ClaimsPrincipal(YayNayIdentity.Create(profile, TestScheme));
            var ticket    = new AuthenticationTicket(principal, TestScheme);

            return(AuthenticateResult.Success(ticket));
        }
Exemplo n.º 2
0
        private async Task OnTokenValidated(TokenValidatedContext arg)
        {
            var provider = arg.HttpContext.RequestServices.GetRequiredService <IPersonProjectionStore>();
            var profile  = await provider.GetProfileAsync(arg.Scheme.Name, arg.Principal.FindFirstValue("sub"));

            if (profile == null)
            {
                arg.Fail("Unable to find a valid userId");
                return;
            }

            arg.Principal = new ClaimsPrincipal(YayNayIdentity.Create(profile, arg.Scheme.Name));
        }