还没有发现任何内置于 blazor 中的东西,所以现在看来你必须使用 jsinterop。还没有真正在 Blazor 中尝试过,但我希望它主要发生在 js 代码中。有一个很好的网站就是为了这个,因为你必须添加一些选项并找出非对称密钥:https://webauthn.guide/
可能会尽快尝试,看看是否有更压缩的方式来呈现 MCVE - 如果我弄清楚了,我会更新它。
更新
试过了,这会为我打开对话框。
实际调用发生在 JS 中:
async function doIt(options) {
var newCreds = await navigator.credentials.create({ publicKey: options });
console.log("Created new credentials:", newCreds);
return newCreds;
}
window.doIt = doit;
在我的剃须刀上放一个按钮:
<button @onclick="DoIt">Do it</button>
然后是@code块中对应的方法和类型:
private async void DoIt()
{
var credOptions = new PublicKeyCredentialCreationOptions();
Console.WriteLine("Sending options for " + credOptions.user.displayName);
var cred = await Js.InvokeAsync<PublicKeyCredential>("doIt", credOptions);
Console.WriteLine("Received cred");
Console.WriteLine(cred);
}
// Do all the things (I think this is like pattern/faceID/touchID/etc)
static readonly int[] Algs = { -7, -8, -35, -36, -37, -38, -39, -257, -258, -259, -65535 };
private static PublicKeyCredentialCreationOptions.PublicKeyCredentialParameters Alg(int alg)
{
return new PublicKeyCredentialCreationOptions.PublicKeyCredentialParameters("public-key", alg);
}
public class PublicKeyCredentialCreationOptions
{
public byte[] challenge { get; set; } = DateTime.Now.ToString().Select(c => (byte)c).ToArray(); // Just random stuff here I think?
public RelyingParty rp { get; set; } = new ("WebAuthnExample"); // If I understand correctly, id will be auto filled with the current domain
public User user { get; set; } =
new("metallkiller".Select(c => (byte)c).ToArray(), "metallkiller", "Metallkiller");
public PublicKeyCredentialParameters[] pubKeyCredParams { get; set; } = Algs.Select(Alg).ToArray();
public long timeout { get; set; } = 60000; // Not entirely sure what this does - docs has more info
public string attestation { get; set; } = "direct"; // No idea I just copied this
public record RelyingParty(string name);
public record User(byte[] id, string name, string displayName);
public record PublicKeyCredentialParameters(string type, int alg);
}
好消息:这将打开生物识别身份验证对话框并创建凭证。
坏消息:无法将凭据返回到 dotnet,也许我忘了做一些 jsinterop-magic 或者它只是不适用于凭据,那么我们可能必须读取 JS 中的所有内容并将它们返回到我们自己的对象中或者其他的东西。如果有人告诉我这里发生了什么,我将不胜感激。
编辑:返回类型的来源:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e57ff15825e1a05c923f80f39dbb7966d20db950/types/webappsec-credential-management/index.d.ts