当前位置: 首页>>代码示例>>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;未经允许,请勿转载。