dotnet add package Passwordless
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
private IConfiguration Configuration { get; }
...
// 该方法由 runtime 调用。使用此方法向容器添加服务。
// 有关如何配置应用程序的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
// 添加对路由到控制器的支持
services.AddControllers();
// 注入 Passwordless SDK
services.Configure<PasswordlessOptions>(Configuration.GetRequiredSection("Passwordless"));
services.AddPasswordlessSdk(options =>
{
Configuration.GetRequiredSection("Passwordless").Bind(options);
});
}
[HttpGet("/create-token")]
public async Task<IActionResult> GetRegisterToken(string alias)
{
var userId = Guid.NewGuid().ToString();
var payload = new RegisterOptions(userId, alias)
{
Aliases = new HashSet<string> { alias }
};
try
{
var token = await _passwordlessClient.CreateRegisterTokenAsync(payload);
return Ok(token);
}
catch (PasswordlessApiException e)
{
return new JsonResult(e.Details)
{
StatusCode = (int?)e.StatusCode,
};
}
}
[HttpGet("/verify-signin")]
public async Task<IActionResult> VerifySignInToken(string token)
{
try
{
var verifiedUser = await _passwordlessClient.VerifyTokenAsync(token);
return Ok(verifiedUser);
}
catch (PasswordlessApiException e)
{
return new JsonResult(e.Details)
{
StatusCode = (int?)e.StatusCode
};
}
}