- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在输入时尝试构建的示例应用程序的说明
dotnet restore
在命令行。
dotnet --version
报告
2.1.300
当我这样做时,出现以下错误
MSBUILD : error MSB1003: Specify a project or solution file.
The current working directory does not contain a project or solution file.
我在其中执行命令的文件夹包含 .cs 文件但没有 .sln 或 .csproj 文件。
.NetCore 是否需要 .csproj 文件?
代码来自对my question here的回答但github项目已经被删除。
我确实尝试创建一个 .csproj 文件,但我无法猜测要放入其中的包。
[更新]
我添加了以下.csproj 文件
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
</Project>
然后我得到以下命名空间丢失
Microsoft.IdentityModel
Azure
KeyVaultClient
ClientAssertinCertificate
Newtonsoft
Org
我知道如何使用包管理器,但我如何找出所有内容的正确版本?
这里是program.cs
using System;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure.KeyVault;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
namespace dotnetconsole
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(@"This Application must be run after running the powershell script Setup.ps1!
This DotNet Console Application authenticates to Key Vault!
It also creates a Secret Key Value Pair!
And then it gets the Secret Key Value Pair!");
bool isWindows = System.Runtime.InteropServices.RuntimeInformation
.IsOSPlatform(OSPlatform.Windows);
string KEYVAULT_URI = String.Empty;
string APPLICATION_ID = String.Empty;
string CERT_THUMBPRINT = String.Empty;
if(isWindows)
{
KEYVAULT_URI = System.Environment.GetEnvironmentVariable("VAULT_NAME", EnvironmentVariableTarget.User);
APPLICATION_ID = System.Environment.GetEnvironmentVariable("APPLICATION_ID", EnvironmentVariableTarget.User);
CERT_THUMBPRINT = System.Environment.GetEnvironmentVariable("CERT_THUMBPRINT", EnvironmentVariableTarget.User);
}
else
{
var result = GetVariablesFromJSON();
APPLICATION_ID = result.Item1;
CERT_THUMBPRINT = result.Item2;
KEYVAULT_URI = result.Item3;
}
KeyVault keyVaultObj = new KeyVault(APPLICATION_ID, CERT_THUMBPRINT);
var VaultName = "https://" + KEYVAULT_URI + ".vault.azure.net/";
var waitHandle = keyVaultObj.CreateSecretKeyValuePair(VaultName);
Console.WriteLine("Vault URI is! {0}", VaultName);
Console.WriteLine("Wait method is invoked to wait for Secret Key Value pair to be created");
waitHandle.Wait();
Console.WriteLine("Secret Key Value pair is now created");
keyVaultObj.GetResult(VaultName);
}
private static Tuple<string, string, string> GetVariablesFromJSON()
{
var ServicePrincipalJSON = Directory.GetCurrentDirectory() + "/ServicePrincipal.json";
var CertThumbprintJSON = Directory.GetCurrentDirectory() + "/CertThumbprint.txt";
var VaultJSON = Directory.GetCurrentDirectory() + "/KeyVault.json";
if(File.Exists(ServicePrincipalJSON) && File.Exists(CertThumbprintJSON) && File.Exists(VaultJSON))
{
return new Tuple<string, string, string>(ProcessFile(ServicePrincipalJSON, "appId", true), ProcessFile(CertThumbprintJSON, "", false), ProcessFile(VaultJSON, "name", true));
}
return new Tuple<string, string, string>("", "", "");
}
private static string ProcessFile(string fileName, string valueToLookFor, bool isJson)
{
var result = "";
using (StreamReader ContentsOfFile = File.OpenText(fileName))
{
if(isJson){
var stuff = (JObject)JsonConvert.DeserializeObject(ContentsOfFile.ReadToEnd());
result = stuff[valueToLookFor].Value<string>();
}
else {
var contents = ContentsOfFile.ReadToEnd();
contents = contents.Split("=")[1];
result = Regex.Replace(contents, @"\t|\n|\r", "");
}
}
return result;
}
}
}
这里是Util.cs
using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
public class Util
{
public static X509Certificate2 ConvertFromPfxToPem(string filename)
{
using (System.IO.FileStream fs = System.IO.File.OpenRead(filename))
{
byte[] data = new byte[fs.Length];
byte[] res = null;
fs.Read(data, 0, data.Length);
if (data[0] != 0x30)
{
res = GetPem("CERTIFICATE", data);
}
X509Certificate2 x509 = new X509Certificate2(res); //Exception hit here
return x509;
}
}
private static byte[] GetPem(string type, byte[] data)
{
string pem = Encoding.UTF8.GetString(data);
string header = String.Format("-----BEGIN {0}-----", type);
string footer = String.Format("-----END {0}-----", type);
int start = pem.IndexOf(header) + header.Length;
int end = pem.IndexOf(footer, start);
string base64 = pem.Substring(start, (end - start));
base64 = base64.Replace(System.Environment.NewLine, "");
base64 = base64.Replace('-', '+');
base64 = base64.Replace('_', '/');
return Convert.FromBase64String(base64);
}
public static RSACryptoServiceProvider PemFileReader(){
RsaPrivateCrtKeyParameters keyParams;
using (var reader = File.OpenText("cert.pem")) // file containing RSA PKCS1 private key
{
keyParams = ((RsaPrivateCrtKeyParameters)new PemReader(reader).ReadObject());
}
RSAParameters rsaParameters = new RSAParameters();
rsaParameters.Modulus = keyParams.Modulus.ToByteArrayUnsigned();
rsaParameters.P = keyParams.P.ToByteArrayUnsigned();
rsaParameters.Q = keyParams.Q.ToByteArrayUnsigned();
rsaParameters.DP = keyParams.DP.ToByteArrayUnsigned();
rsaParameters.DQ = keyParams.DQ.ToByteArrayUnsigned();
rsaParameters.InverseQ = keyParams.QInv.ToByteArrayUnsigned();
rsaParameters.D = keyParams.Exponent.ToByteArrayUnsigned();
rsaParameters.Exponent = keyParams.PublicExponent.ToByteArrayUnsigned();
RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(2048);
rsaKey.ImportParameters(rsaParameters);
return rsaKey;
}
}
这是KeyVault.cs
using System;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Azure.KeyVault;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure.KeyVault.Models;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
namespace dotnetconsole
{
public class KeyVault
{
KeyVaultClient _keyVaultClient;
string APPLICATION_ID, CERT_THUMBPRINT;
public KeyVault(string APPLICATION_ID, string CERT_THUMBPRINT) {
this.APPLICATION_ID = APPLICATION_ID;
this.CERT_THUMBPRINT = CERT_THUMBPRINT;
_keyVaultClient = new KeyVaultClient(this.GetAccessToken);
}
public static ClientAssertionCertificate AssertionCert { get; set; }
// This method is used to get a token from Azure Active Directory.
public async Task<string> GetAccessToken(string authority, string resource, string scope)
{
var context = new AuthenticationContext(authority, TokenCache.DefaultShared);
bool isWindows = System.Runtime.InteropServices.RuntimeInformation
.IsOSPlatform(OSPlatform.Windows);
X509Certificate2 certByThumbprint = new X509Certificate2();
if(isWindows){
certByThumbprint = FindCertificateByThumbprint(this.CERT_THUMBPRINT);
} else {
// If it's a pem file then we take the private key portion and create a
// RSACryptoServiceProvider and then we create a x509Certificate2 class from the cert portion
// and then we combine them both to become one x509Certificate2
RSACryptoServiceProvider rsaCryptoServiceProvider = Util.PemFileReader();
certByThumbprint = Util.ConvertFromPfxToPem("cert.pem");
certByThumbprint = certByThumbprint.CopyWithPrivateKey(rsaCryptoServiceProvider);
}
AssertionCert = new ClientAssertionCertificate(this.APPLICATION_ID, certByThumbprint);
var result = await context.AcquireTokenAsync(resource, AssertionCert);
return result.AccessToken;
}
public async Task CreateSecretKeyValuePair(string vaultBaseURL)
{
System.Console.WriteLine("Authenticating to Key Vault using ADAL Callback to create Secret Key Value Pair");
System.Console.WriteLine(vaultBaseURL);
KeyVaultClient kvClient = new KeyVaultClient(this.GetAccessToken);
await kvClient.SetSecretAsync(vaultBaseURL, "TestKey", "TestSecret");
}
// In this method we first get a token from Azure Active Directory by using the self signed cert we created in our powershell commands
// And then we pass that token to Azure Key Vault to authenticate the service principal to get access to the secrets
// Finally we retrieve the secret value that was created previously
public void GetResult(string keyvaultUri)
{
try
{
var result = this._keyVaultClient.GetSecretAsync(keyvaultUri, "TestKey").Result.Value;
System.Console.WriteLine("Secret Key retrieved is {0} and value is {1}, ", "TestKey", result);
}
catch (System.Exception ex)
{
throw ex;
}
}
// In Windows this method would find the certificate that's stored in the certificate manager under current user
// Given a thumbprint this method finds the certificate
public static X509Certificate2 FindCertificateByThumbprint(string findValue)
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
try
{
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection col = store.Certificates.Find(X509FindType.FindByThumbprint,
findValue, false); // Don't validate certs, since the test root isn't installed.
if (col == null || col.Count == 0 )
return null;
return col[0];
}
finally
{
store.Close();
}
}
}
}
[更新]现在我可以运行 dotnet restore 但 dotnet run 出现错误
如下
KeyVault.cs(2,17): error CS0234: The type or namespace name 'IdentityModel' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
KeyVault.cs(3,17): error CS0234: The type or namespace name 'Azure' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
KeyVault.cs(6,17): error CS0234: The type or namespace name 'Azure' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
Program.cs(3,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
Program.cs(4,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
Program.cs(10,17): error CS0234: The type or namespace name 'Azure' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
Program.cs(11,17): error CS0234: The type or namespace name 'IdentityModel' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
Util.cs(7,7): error CS0246: The type or namespace name 'Org' could not be found (are you missing a using directive or an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
Util.cs(8,7): error CS0246: The type or namespace name 'Org' could not be found (are you missing a using directive or an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
Util.cs(9,7): error CS0246: The type or namespace name 'Org' could not be found (are you missing a using directive or an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
Util.cs(10,7): error CS0246: The type or namespace name 'Org' could not be found (are you missing a using directive or an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
Util.cs(11,7): error CS0246: The type or namespace name 'Org' could not be found (are you missing a using directive or an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
KeyVault.cs(22,23): error CS0246: The type or namespace name 'ClientAssertionCertificate' could not be found (are you missing a using directive or an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
KeyVault.cs(14,9): error CS0246: The type or namespace name 'KeyVaultClient' could not be found (are you missing a using directive or an assembly reference?) [C:\dev2018\key-vault-dotnet-quickstart\MyKeyVault.csproj]
The build failed. Please fix the build errors and run again.
[更新]
工具 ->Nuget 包管理器 -> 解决方案的管理包报告错误
Microsoft Visual Studio
The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
[更新]
我保存了所有内容,关闭了 .sln 文件并重新打开了它。然后我能够进入 Nuget 包管理器。
[更新]
我已经安装了 Microsoft.Azure.KeyVault(3.0.0) 和 Newtonsoft.Json(11.0.2)我在使用 Microsoft.IdentityModel.Clients.ActiveDirectory 时遇到问题当我尝试 Microsoft.IdentityModel 时,它是错误的框架。
Package 'Microsoft.IdentityModel 6.1.7600.16394' was restored using ''.NETFramework, Version=v4.61'
instead of the projecttargetframework '.NETCoreApp,Version=v2.1'
This package may not be fully compatible with your project
[更新]用 Google 搜索“使用 Microsoft.IdentityModel.Clients.ActiveDirectory 核心”
找到 this link并在 PM 运行
Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 3.19.8
[更新]尝试使用语句搜索 Bouncy CaSTLe 并找到
Install-Package BouncyCastle.NetCore -Version 1.8.2
[更新]
Rebuild All成功,现在我在第47行出现运行时错误
var waitHandle = keyVaultObj.CreateSecretKeyValuePair(VaultName);
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred.
Source=System.Private.CoreLib
StackTrace:
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at dotnetconsole.Program.Main(String[] args) in C:\dev2018\key-vault-dotnet-quickstart\Program.cs:line 47
Inner Exception 1:
ArgumentNullException: Value cannot be null.
最佳答案
错误的发生是因为我没有.csproj 文件,作者没有将它包含在源代码中。这个问题记录了我构建一个问题所经历的步骤。
关于c# - MSBUILD : error MSB1003: Specify a project or solution file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51393395/
我希望将 LSB、MSB 的字节数组转换为 int 的数组 目前,我正在使用 for 循环并单独转换每组值, void ConvertToInt(int OutArray[], byte InArra
我想分配一个 std_logic_vector 而不给出界限。像这样: constant xy: std_logic_vector := "100111"; 当我想访问单个位时: xy(0), xy(
我检查了用于反转位顺序的 SWAR 算法(SIMD W在 A Rregister 中) unsigned int 的。 signed int 有类似的东西吗? 最佳答案 该算法仅适用于无符号整数,因为
当我使用下面的代码更改 32 位时,或者当 bitc 等于 31 时,它给出 -2147483643。 它似乎将所有 0 更改为 1,而不是仅将最后一个 0 更改。我怎样才能在代码中解决这个问题?预先
对于给定的数字 unsigned int a = 1203; 仅对上述示例增加最重要的小数位 a = 2203; 如何实现? 我是这样开始的 for (n=a; n; n/=10){ b = n%10
以unsigned char为例。 它的范围是0 到 255。 如果我尝试在其中存储 256,将发生以下情况:- 256 的二进制文件 100000000 由于它由9位组成,因此MSB 1将被丢弃,它
我正在通过将位打包到一个 uint 数组中来实现一个位向量。 getBit(index)函数执行 (array[cell] & (1 > bit获取是否已设置位。这适用于除 MSB 之外的所有位。它不
我想知道一个字节的第一位是什么值。 例如: 我有 byte m = (byte) 0x8C; 我怎么知道第一位是 1 还是 0? 谁能帮帮我? 最佳答案 这取决于你所说的“第一位”是什么意思。如果您的
我需要将十六进制字符串中的 MSB 和其余位分开。例如:我有一个十六进制字符串 a2,相当于 1010 0010。我想分离出 MSB(在本例中为 1),其余数字转换为十进制。我想我可以做这样的事情:
我有一个 20 个字/字节的数组,存储一个 160 位数。如何找到从 msb 开始的第一个非零位。我需要找到位的位置,然后相应地从第一个'1'位置开始我需要做一些操作。 最佳答案 如果您使用的是 gc
我有一个函数返回 1 Byte uint8_t fun(); 该函数应该运行 9 次,所以我得到 9 字节 我想将最后一个 8 作为 4 short values 这里我所做的,但是我'我不确定我得到
我正在努力转换在特定微 Controller 上运行的程序,并使其适应在树莓派上运行。我已经成功地从我一直在使用的传感器中提取值,但现在我遇到了一个问题,我认为这是由我无法理解的几行代码引起的。我已经
所以我想切换我数字的最高有效位。这是一个例子: x = 100101 then answer should be 00101 我有一台 64 位机器,因此我不希望答案是 100000....10010
给定一个 len 类型的 signed short 元素数组,它是在数组中的最大绝对值元素中找到设置的最高有效位的位置。例如,如果数组 L 包含 {-134, 123, 0, -890} 那么 f(L
在中断子程序(每 5 µs 调用一次)中,我需要检查一个字节的 MSB 并将其复制到该字节的其余部分。 我需要做类似的事情: if(MSB == 1){byte = 0b11111111} else{
我正在交叉编译 MIPS 处理器(little-endian arch)的开源库 oRTP。我的开发系统是i386 linux。我将配置脚本运行为 ./configure --host=mips-li
在两个有符号整数的二进制加法中,如果进出 MSB 列的进位不匹配,则会出现有符号溢出。 这条规则背后的逻辑是什么?为什么进位和执行应该匹配才能得到正确的结果。请解释一下。 最佳答案 显然有两种不匹配的
我有一个二进制协议(protocol),它将每个有效负载字节的 MSB 提取到 MSB 集合字节(七进制)中进行传输,并在接收器端重新注入(inject) MSB。有效负载由 n 个四字节帧组成,具体
我有以下代码用于从非负整数获取 MSB(最高有效位),更具体地说是 Int32: private static readonly int[] powersOf2 = new int[]
我以 2 字节短值的十六进制值形式获取数据,但交换值后丢失了。 signed short value = 0x0040; value = (value*0.5) - 40; convertMSB
我是一名优秀的程序员,十分优秀!