Beispiel #1
0
        public UserInformationMiddleware(
            RequestDelegate next,
            IApplicationBuilder app,
            IOptions <TOptions> options)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _next       = next;
            _options    = options.Value;
            _httpClient = new HttpClient(_options.BackChannelHttpHandler ?? new HttpClientHandler());

            var nullAuthenticationBuilder = app.New();
            var nullAuthenticationOptions = new NullAuthenticationOptions
            {
                AutomaticAuthenticate = true,
                AutomaticChallenge    = true
            };

            nullAuthenticationBuilder.UseMiddleware <NullAuthenticationMiddleware>(Options.Create(nullAuthenticationOptions));
            nullAuthenticationBuilder.Run(ctx => next(ctx));
            _nullAuthenticationNext = nullAuthenticationBuilder.Build();
        }
        private async Task <Dictionary <string, string> > GetUserInformationResponse(
            UserInformationOptions userInformationOptions,
            string token)
        {
            Uri uri = null;
            var url = userInformationOptions.UserInformationEndPoint;

            if (!Uri.TryCreate(url, UriKind.Absolute, out uri))
            {
                throw new ArgumentException(ErrorDescriptions.TheUserInfoEndPointIsNotAWellFormedUrl);
            }

            var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);

            requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            requestMessage.Headers.Add(AuthorizationName, string.Format("Bearer {0}", token));
            var response = await _httpClient.SendAsync(requestMessage);

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            var content = await response.Content.ReadAsStringAsync();

            return(JsonConvert.DeserializeObject <Dictionary <string, string> >(content));
        }
        public static IApplicationBuilder UseAuthenticationWithUserInformation(
            this IApplicationBuilder app,
            UserInformationOptions userInformationOptions)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (userInformationOptions == null)
            {
                throw new ArgumentNullException(nameof(userInformationOptions));
            }

            return(app.UseMiddleware <UserInformationMiddleware <UserInformationOptions> >(app, Options.Create(userInformationOptions)));
        }