我在这件事上挣扎的时间比我要承认的时间还要长。我只是想用WebAutn注册一个新设备。我至少尝试过两个不同的例子,并且研究了许多不同的代码。我好像把一切都做对了..。但不管我做什么我都会得到同样的无益的例外。我已经搜索过网络,似乎没有其他人得到这个例外,所以我真的不知道是怎么回事?
简而言之,在线newCredential =等待navigator.credentials.create({publicKey: options});我得到了以下错误:“异常:TypeError:未能在‘CredentialsContainer’上执行'create‘:提供的值'2’不是AttestationConveyancePreference类型的有效枚举值。”
这里是返回的PublicKeyCredentialCreationOptions,我正在提交:注意:挑战和user.id都被填充了,但是由于它们是ArrayBuffer()的,所以不会打印。
{
"rp": {
"id": "someserver",
"name": "IB-Fido2",
"icon": null
},
"user": {
"name": "fred@someserver.com",
"id": {},
"displayName": "Fred Sampson"
},
"challenge": {},
"pubKeyCredParams": [
{
"type": 0,
"alg": -7
},
{
"type": 0,
"alg": -257
},
{
"type": 0,
"alg": -37
},
{
"type": 0,
"alg": -35
},
{
"type": 0,
"alg": -258
},
{
"type": 0,
"alg": -38
},
{
"type": 0,
"alg": -36
},
{
"type": 0,
"alg": -259
},
{
"type": 0,
"alg": -39
}
],
"timeout": 60000,
"attestation": 0,
"authenticatorSelection": {
"requireResidentKey": false,
"userVerification": 1
},
"excludeCredentials": [],
"extensions": null,
"status": "ok",
"errorMessage": ""
}如有任何帮助或建议,将不胜感激!
更新:已经序列化/反序列化对象有一段时间了.但几乎总是为了C#而不是JavaScript..。因此,直到我遇到这个问题,我才意识到JavaScripts字符串Enum。我看到这是一个转换错误,但错误地认为Fido2NetLib必须返回所需的对象,因为没有其他人用它报告错误。
谢天谢地,Newtonsoft.Json已经有了扩展来完成这种转换,我从here中发现了这一点。
因此,最后,我必须使用StringEnumConverter()序列化对象,然后将其反序列化为我从json创建的自定义类,这个类与字符串Enum值完全相同。我需要这样做,因为把Json传给Blazor.Interop不管用.它需要一个实际的物体。最后,我确信有一种更干净的方法来完成字符串Enum的转换,但是我想继续前进,我现在正在使用它。请参见:
string url = string.Format("{0}{1}", Endpoints.UsersFidoOptions, userId);
var options = await userRepository.FidoOptions(url);
if (options != null)
{
if (options.Status != "ok")
{
//Error Message
}
else
{
try
{
// Serialize returned options: NOTE: String Enum Converter necessary as this object has JavaScript String Enum's
string json = JsonConvert.SerializeObject(options, new Newtonsoft.Json.Converters.StringEnumConverter());
// Next Deserialize into Custom Object to Compensate for String Enum's
var pubKey = Newtonsoft.Json.JsonConvert.DeserializeObject<PublicKey>(json);
// Now Serialize the Public Key for Session Storage
string pkJson = JsonConvert.SerializeObject(pubKey, new Newtonsoft.Json.Converters.StringEnumConverter());
await sessionStorage.SetItemAsync<string>("fido2.attestationOptions", pkJson);
await jsRuntime.InvokeVoidAsync("blazorInterop.registerOptions", pubKey);
}
catch (JSException e)
{
string err = e.Message;
}
}
}发布于 2021-04-17 14:05:43
构建此JSON时,它插入的是枚举数值,而不是字符串值。例如,在pubKeyCredParams中,所有类型值都应该是“公钥”,而不是0,认证应该是“无”而不是0,而userVerification应该是“首选”,而不是1。在规范https://www.w3.org/TR/webauthn/中,所有这些都定义为DOMString。
https://stackoverflow.com/questions/67129650
复制相似问题