简体   繁体   English

如果 SSL 证书不能在 IIS 中工作,但在本地工作,我该怎么办

[英]What can i do if SSL certificate don`t working in IIS, but working in Local

I have two projects: Web Api(back) and MVC(front).我有两个项目:Web Api(背面)和 MVC(正面)。 Web Api(back) has a method that uses the SSL certificate. Web Api(back) 有一个使用 SSL 证书的方法。 If running the Web Api(back) in local, then the method works, but if running Web Api(back) in IIS and send post in MVC(front) to the method, then it catches an error "One or more errors occurred".如果在本地运行 Web Api(back),则该方法有效,但如果在 IIS 中运行 Web Api(back),则在 MVC 中运行“或更多错误”,然后将其发送到 MVC() . (The SSL connection could not be established, see inner exception.). (无法建立 SSL 连接,请参阅内部异常。)。 Web Api(back) code Web Api(背面)代码

public decimal GetIdentification(IdentificationModel identification, int key)
    {
        try
        { 
        var handler = new HttpClientHandler();
        var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"cert\\hgg.p12");
        handler.ClientCertificates.Add(new X509Certificate2(path, _certPassword));

        using (var httpClient = new HttpClient(handler))
        {
            using (var request = new HttpRequestMessage(new HttpMethod("POST"), _identityUrl + "iin=" + identification.IIN + "&vendor=" + _vendor))
            {
                string token = GetToken();
                request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + token);
                request.Headers.TryAddWithoutValidation("x-idempotency-key", "key:" + "hggKey-" + key);
                request.Method = new HttpMethod("POST");
                //request.Content = new StringContent(identification.Photo);
                request.Content = new StreamContent(new MemoryStream(Convert.FromBase64String(identification.Photo)));
                request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("image/png");

                var response = httpClient.SendAsync(request);
                string data = response.Result.Content.ReadAsStringAsync().Result;
                return JsonConvert.DeserializeObject<dynamic>(data).result;
            }
        }
        }
        catch (Exception e)
        {
            throw new Exception("GetIdentification" + e.Message);
        }
    }
    public string GetToken()
    {
        var handler = new HttpClientHandler();
        var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cert\\hgg.p12");
        handler.ClientCertificates.Add(new X509Certificate2(path, _certPassword));
        try
        {
            

            using (var httpClient = new HttpClient(handler))
            {
                using (var request = new HttpRequestMessage(new HttpMethod("POST"), _tokenUrl))
                {
                    request.Headers.TryAddWithoutValidation("Authorization", "Basic " + _token);
                    request.Method = new HttpMethod("POST");
                    request.Content = new StringContent("grant_type=password&username=" + _username + "&password=" + _password + "&scope=identkey");
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");

                    var response = httpClient.SendAsync(request);

                    return JsonConvert.DeserializeObject<dynamic>(response.Result.Content.ReadAsStringAsync().Result).access_token;
                }
            }
        }
        catch(Exception e)
        {
            throw new Exception("GetToken" + e.Message + path + " " +_certPassword);
        }
    }
}

MVC(front) MVC(前)

public string Identity(RequestClass<IdentificationModel> request)
    {            
        ResponseClass<decimal> response = new ResponseClass<decimal>();
        using (var httpClient = new HttpClient())
        {
            var json = JsonConvert.SerializeObject(request);
            var data = new StringContent(json, Encoding.UTF8, "application/json");
            var httpResponse =
                    httpClient.PostAsync(_apiUrl + $"Identification", data).Result;
            string responseContent = httpResponse.Content.ReadAsStringAsync().Result;
            
            return responseContent;
        }
    }

ApplicationPoolIdentity don't have permission for certmgr.msc ApplicationPoolIdentity 没有 certmgr.msc 的权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将WCF与SSL证书HTTPS IIS一起使用 - How can I use a WCF with a SSL Certificate HTTPS IIS SignalR在本地IIS上不起作用,但在IIS Express上起作用 - SignalR not working on local IIS but working with IIS Express Web应用程序在VS Development Server中工作但在IIS中不起作用 - Web Application working in VS Development Server but don't work in IIS 代码在本地环境中工作,但在 iis 上托管时不起作用 - Code working in local environment, but didn't work when hosted on iis 如何在不切换到 IIS 的情况下将 SSL 证书附加到我的 asp dot net 网站? - How do I attach an SSL certificate to my asp dot net website without switching to IIS? 为什么 UpdatePanel 可以在本地 IIS 上运行,而不能在远程 IIS 上运行? - Why is UpdatePanel working on local IIS but not on remote IIS? 如何在IIS 8上通过一个IP:PORT使用多个SSL证书 - How can i use multiple SSL certificate with one IP:PORT on IIS 8 自动IIS6 403.4重定向到SSL无效 - Automatic IIS6 403.4 redirect to SSL not working 文件上载在IIS Express中有效,但在本地IIS上无效 - The file upload working in IIS express but not working on local IIS 我无法访问在 IIS 上运行的特定本地 Web 应用程序 - I can't reach a particular local web app that is running on IIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM