Auth0 LoginAsync 错误
Auth0 LoginAsync error
我必须执行 Auth0 身份验证过程并提取令牌。
我有 Authenticator class 如下 -
class Auth0Authenticator
{
public Auth0Authenticator() { performAuthentication(); }
public void performAuthentication()
{
Auth0Client auth0Client = new Auth0Client(new Auth0ClientOptions()
{
Domain = "mydomain",
ClientId = "clientid",
});
var extraParameters = new Dictionary<string, string>();
extraParameters.Add("connection", "parameter");
var result = auth0Client.LoginAsync(extraParameters: extraParameters);
}
}
在执行 LoginAsync 时出现错误 - 调用线程必须是 STA,因为许多 UI 组件需要这个。
即使在创建 STA 线程或添加属性 [STAThread] 后也无济于事。
当我在一个简单的基于对话框的应用程序中执行相同的代码时,代码成功地向我返回了令牌。但是在我的项目中放置相同的代码(由 MFC/C#/CLI 组成)抛出错误。
有人可以帮忙吗?
这可能是 XY problem。 Auth0Client.LoginAsync
是一个异步 API,您正试图在 class 的构造函数中调用它。如果依赖于该功能的代码在能够执行其功能之前完成,这可能会产生负面影响。
重构代码以遵循建议的语法
public class Auth0Authenticator {
public Auth0Authenticator() {
//Subscribe to the event
autoAuthenticate += onAutoAuthenticating();
//raise event to allow async operation.
autoAuthenticate(this, EventArgs.Empty);
}
private event EventHandler autoAuthenticate = delegate { };
private async void onAutoAuthenticating(object sender, EventArgs args) {
await PerformAuthenticationAsync();
}
public async Task PerformAuthenticationAsync() {
Auth0Client auth0Client = new Auth0Client(new Auth0ClientOptions() {
Domain = "mydomain",
ClientId = "clientid",
});
var extraParameters = new Dictionary<string, string>();
extraParameters.Add("connection", "parameter");
var result = await auth0Client.LoginAsync(extraParameters: extraParameters);
//...do something with the result as needed
string access_token = result.AccessToken;
string refresh_token = result.RefreshToken;
//...
}
}
我必须执行 Auth0 身份验证过程并提取令牌。
我有 Authenticator class 如下 -
class Auth0Authenticator
{
public Auth0Authenticator() { performAuthentication(); }
public void performAuthentication()
{
Auth0Client auth0Client = new Auth0Client(new Auth0ClientOptions()
{
Domain = "mydomain",
ClientId = "clientid",
});
var extraParameters = new Dictionary<string, string>();
extraParameters.Add("connection", "parameter");
var result = auth0Client.LoginAsync(extraParameters: extraParameters);
}
}
在执行 LoginAsync 时出现错误 - 调用线程必须是 STA,因为许多 UI 组件需要这个。
即使在创建 STA 线程或添加属性 [STAThread] 后也无济于事。
当我在一个简单的基于对话框的应用程序中执行相同的代码时,代码成功地向我返回了令牌。但是在我的项目中放置相同的代码(由 MFC/C#/CLI 组成)抛出错误。
有人可以帮忙吗?
这可能是 XY problem。 Auth0Client.LoginAsync
是一个异步 API,您正试图在 class 的构造函数中调用它。如果依赖于该功能的代码在能够执行其功能之前完成,这可能会产生负面影响。
重构代码以遵循建议的语法
public class Auth0Authenticator {
public Auth0Authenticator() {
//Subscribe to the event
autoAuthenticate += onAutoAuthenticating();
//raise event to allow async operation.
autoAuthenticate(this, EventArgs.Empty);
}
private event EventHandler autoAuthenticate = delegate { };
private async void onAutoAuthenticating(object sender, EventArgs args) {
await PerformAuthenticationAsync();
}
public async Task PerformAuthenticationAsync() {
Auth0Client auth0Client = new Auth0Client(new Auth0ClientOptions() {
Domain = "mydomain",
ClientId = "clientid",
});
var extraParameters = new Dictionary<string, string>();
extraParameters.Add("connection", "parameter");
var result = await auth0Client.LoginAsync(extraParameters: extraParameters);
//...do something with the result as needed
string access_token = result.AccessToken;
string refresh_token = result.RefreshToken;
//...
}
}