我写了一个小的ASP.NET Core 2应用程序.它作为服务运行,因此没有IIS.它在运行Windows 7 SP1的PC上运行.
var host = WebHost.CreateDefaultBuilder(args)
.UseContentRoot(pathToContentRoot)
.UseHttpSys(options =>
{
options.Authentication.Schemes = AuthenticationSchemes.None;
options.Authentication.AllowAnonymous = true;
options.MaxConnections = null;
options.MaxRequestBodySize = 30000000;
options.UrlPrefixes.Add("http://*:5050");
})
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
if (isService)
{
host.RunAsService();
}
else
{
host.Run();
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我想听5050端口.没有SSL就可以正常工作.
我的问题是,如何为我的应用程序启用https?再次:没有IIS,没有域名(没有互联网连接).通信就在内部网络内部,因此我想使用自签名证书.
我阅读了文档(HTTP.sys文档 ; Netsh命令 ; New-SelfSignedCertificate),但总有一些与我的情况不同(他们使用Krestel,或者是使用IIS).另外,我不知道如何为我的应用程序获取App-ID(netsh所需).我试过这个:StackOverflow获取GUID但它不起作用.
var assembly = typeof(Program).Assembly;
// following line produces: System.IndexOutOfRangeException
var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
var id = attribute.Value;
Console.WriteLine(id);
Run Code Online (Sandbox Code Playgroud)
所以我对所有可能性和不同配置感到有点困惑.文档不考虑我的具体情况.
我创建了一个证书,我想我需要将它存储在"我的"商店中.(那是什么?cert:\ LocalMachine\My)然后我需要将我的Applicaion ID和端口分配给它.
但我不知道该怎么做.有人可以帮忙吗?
我想要对URL进行POST调用,并且作为响应,我只得到一个字符串“ ok”或“ no”。
public interface registerAPI
{
@FormUrlEncoded
@POST("addDevice.php")
Call<String> insertUser(
@Field("name") String devicename,
@Field("username") String regid);
}
Run Code Online (Sandbox Code Playgroud)
所以我只想给POST方法两个参数,我想要一个字符串。在服务器上的PHP脚本中,有类似以下内容:
<?php
if(...)
echo "ok";
else
echo "no";
Run Code Online (Sandbox Code Playgroud)
所以我打电话给我的Android手机:
Retrofit adapter = new Retrofit.Builder()
.baseUrl("http://root.url.net/")
.addConverterFactory(GsonConverterFactory.create()) //I dont want this..
.build();
registerAPI api = adapter.create(registerAPI.class);
Call<String> call = api.insertUser(name,regid);
call.enqueue(new Callback<String>()
{
@Override
public void onResponse(Response<String> response, Retrofit retrofit)
{
Log.i("Error",response.message());
}
@Override
public void onFailure(Throwable t)
{
Log.d("Error", " Throwable is " +t.toString());
}
});
Run Code Online (Sandbox Code Playgroud)
因此,当我在Throwable中运行此命令时,收到以下消息:
Unable to create converter for …
Run Code Online (Sandbox Code Playgroud) 我有一个Xamarin Forms项目,在 MainPage.xaml.cs 文件中我想向我的服务器执行请求。服务器是用ASP.NET Core 2 编写的,并使用自签名证书运行。
购买证书并不能解决我的问题,因为客户不希望它和应用程序只在局域网中运行。
在我的 MainPage.xaml.cs 文件中,Http 请求如下所示:
HttpClient m_Client = new HttpClient();
var uri = new Uri(myURL);
var response = await m_Client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
...
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都很好。如果我在Android上运行应用程序并尝试执行请求,Android 会抛出 SSL 异常,因为找不到我的证书的 CA。
如何使用自签名证书与我的服务器通信?
我查了一下问题并找到了很多解决方案,例如:
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
Run Code Online (Sandbox Code Playgroud)
如果您将此代码添加到 Android 项目中的 MainActivity.cs 文件中,它应该接受所有证书。但这对我不起作用。似乎这个方法永远不会被调用。
对于如何进行沟通有什么建议吗?
问候
我想将 URL 中的所有证书保存到磁盘。例如https://www.google.de
如果我使用 Firefox 浏览此页面,我可以看到三个证书。
使用 Firefox,我可以将它们全部导出,并将它们保存到磁盘上。
所以我想在 C# 中做到这一点。我开始使用以下代码获取证书。
/// <summary>
/// Get and write certificate from URL into file in path
/// </summary>
/// <param name="_URL">URL of website with certficate</param>
/// <param name="_path">Path where you want to store certificate</param>
private static void SaveCertificate(String _URL, String _path)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_URL);
request.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
X509Certificate2 cert = new X509Certificate2(request.ServicePoint.Certificate);
File.WriteAllText(_path, ExportToPEM(cert));
}
catch (Exception)
{
}
}
/// <summary> …
Run Code Online (Sandbox Code Playgroud)