當前位置: 首頁>>代碼示例>>C#>>正文


C# HttpRequestMessage.GetClientCertificate方法代碼示例

本文整理匯總了C#中System.Net.Http.HttpRequestMessage.GetClientCertificate方法的典型用法代碼示例。如果您正苦於以下問題:C# HttpRequestMessage.GetClientCertificate方法的具體用法?C# HttpRequestMessage.GetClientCertificate怎麽用?C# HttpRequestMessage.GetClientCertificate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Net.Http.HttpRequestMessage的用法示例。


在下文中一共展示了HttpRequestMessage.GetClientCertificate方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: SendAsync

        protected override async Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var cert = request.GetClientCertificate();
            if (cert == null) return await base.SendAsync(request, cancellationToken);
            try
            {
                _validator.Validate(cert);
            }
            catch (SecurityTokenValidationException)
            {
                return new HttpResponseMessage(HttpStatusCode.Unauthorized);
            }
            var issuer = _issuerMapper(cert);
            if (issuer == null)
            {
                return new HttpResponseMessage(HttpStatusCode.Unauthorized);
            }

            var claims = ExtractClaims(cert, issuer);
            var identity = new ClaimsIdentity(new ClaimsIdentity(claims, X509AuthnMethod));
            AddIdentityToCurrentPrincipal(identity, request);

            return await base.SendAsync(request, cancellationToken);
        }
開發者ID:wukaixian,項目名稱:WebApiBook.Security,代碼行數:25,代碼來源:X509CertificateMessageHandler.cs

示例2: SendAsync

        protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, 
            CancellationToken cancellationToken)
        {
            var cert = request.GetClientCertificate();
            if (cert != null)
            {
                // Not sure how this ever results in not null. Hope to find out
            }

            return base.SendAsync(request, cancellationToken);
        }
開發者ID:divyang4481,項目名稱:STS,代碼行數:12,代碼來源:CertificateHandler.cs

示例3: SendAsync

        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // Retrieving the Client certificate sent by the client
            X509Certificate cert = request.GetClientCertificate();

            // The following code SHOULD be replaced with some custom mapping logic from client certificate to their roles. 
            // The client certificate is not being checked for certificate revocation or ensure PKI validity. 
            if (cert != null)
            {
                if (cert.GetCertHashString() == Program.ClientCertHash)
                {
                    Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(cert.Subject), new[] { "Administrators" });
                }
            }

            return base.SendAsync(request, cancellationToken);
        }
開發者ID:andreychizhov,項目名稱:microsoft-aspnet-samples,代碼行數:17,代碼來源:CustomCertificateMessageHandler.cs

示例4: SendAsync

        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // Retrieving the Client certificate sent by the client
            X509Certificate cert = request.GetClientCertificate();

            // The following code SHOULD be replaced with some custom mapping logic from client certificate to their roles. 
            if (cert != null)
            {
                if (cert.GetCertHashString() == ClientCertHash)
                {
                    Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(cert.Subject, "X509Certificate"), new[] { "Administrators" });

                    if (HttpContext.Current != null)
                    {
                        HttpContext.Current.User = Thread.CurrentPrincipal;
                    }
                }
            }

            return base.SendAsync(request, cancellationToken);
        }
開發者ID:andreychizhov,項目名稱:microsoft-aspnet-samples,代碼行數:21,代碼來源:CustomCertificateMessageHandler.cs

示例5: Authenticate

        public ClaimsPrincipal Authenticate(HttpRequestMessage request)
        {
            string resourceName = request.RequestUri.AbsoluteUri;

            // if session feature is enabled (and this is not a token request), check for session token first
            if (Configuration.EnableSessionToken && !IsSessionTokenRequest(request))
            {
                var principal = AuthenticateSessionToken(request);

                if (principal.Identity.IsAuthenticated)
                {
                    Tracing.Information(Area.HttpAuthentication, "Client authenticated using session token");
                    return principal;
                }
            }

            // check for credentials on the authorization header
            if (Configuration.HasAuthorizationHeaderMapping)
            {
                var authZ = request.Headers.Authorization;
                if (authZ != null)
                {
                    Tracing.Verbose(Area.HttpAuthentication, "Mapping for authorization header found: " + authZ.Scheme);

                    var principal = AuthenticateAuthorizationHeader(authZ.Scheme, authZ.Parameter);

                    if (principal.Identity.IsAuthenticated)
                    {
                        Tracing.Information(Area.HttpAuthentication, "Client authenticated using authorization header mapping: " + authZ.Scheme);

                        return Transform(resourceName, principal);
                    }
                }
            }

            // check for credentials on other headers
            if (Configuration.HasHeaderMapping)
            {
                if (request.Headers != null)
                {
                    Tracing.Verbose(Area.HttpAuthentication, "Mapping for header header found.");

                    var principal = AuthenticateHeaders(request.Headers);

                    if (principal.Identity.IsAuthenticated)
                    {
                        Tracing.Information(Area.HttpAuthentication, "Client authenticated using header mapping");

                        return Transform(resourceName, principal);
                    }
                }
            }

            // check for credentials on the query string
            if (Configuration.HasQueryStringMapping)
            {
                if (request.RequestUri != null && !string.IsNullOrWhiteSpace(request.RequestUri.Query))
                {
                    Tracing.Verbose(Area.HttpAuthentication, "Mapping for query string found.");

                    var principal = AuthenticateQueryStrings(request.RequestUri);

                    if (principal.Identity.IsAuthenticated)
                    {
                        Tracing.Information(Area.HttpAuthentication, "Client authenticated using query string mapping");
                        return Transform(resourceName, principal);
                    }
                }
            }

            // check for client certificate
            if (Configuration.HasClientCertificateMapping)
            {
                var cert = request.GetClientCertificate();

                if (cert != null)
                {
                    Tracing.Verbose(Area.HttpAuthentication, "Mapping for client certificate found.");

                    var principal = AuthenticateClientCertificate(cert);

                    if (principal.Identity.IsAuthenticated)
                    {
                        Tracing.Information(Area.HttpAuthentication, "Client authenticated using client certificate");
                        return Transform(resourceName, principal);
                    }
                }
            }

            // do claim transformation (if enabled), and return.
            return Transform(resourceName, Principal.Anonymous);
        }
開發者ID:rmgreen85,項目名稱:Thinktecture.IdentityModel.45,代碼行數:92,代碼來源:HttpAuthentication.cs

示例6: TryGetClientCertificateFromRequest

        public bool TryGetClientCertificateFromRequest(HttpRequestMessage request, out X509Certificate2 clientCertificate)
        {
            clientCertificate = request.GetClientCertificate();

            if (clientCertificate != null)
            {
                return true;
            }

            return false;
        }
開發者ID:ram1583,項目名稱:Thinktecture.IdentityServer.v2,代碼行數:11,代碼來源:AuthenticationHelper.cs


注:本文中的System.Net.Http.HttpRequestMessage.GetClientCertificate方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。