auth0 身份验证问题:HttpContext.Current 为空。此代码路径仅在 ASP.NET 的执行上下文中有效
auth0 authentication issue: HttpContext.Current is null. This code path is only valid when in the execution context of ASP.NET
我在我的应用程序中使用 auth0 进行身份验证、角色和权限。我正在按照下面的教程在我的应用程序中实现 auth0,
它为我创建了登录屏幕,在提供电子邮件 ID 和密码后,它导航到 LoginHandler.ashx。它显示在错误页面下方。
我正在获取令牌、用户 ID 和其他信息,但如何解决处理程序中的这个错误?
这是我的解决方案,如果有人遇到同样的问题,
public class LoginCallback : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
AuthenticationApiClient client = new AuthenticationApiClient(
new Uri(string.Format("https://{0}", ConfigurationManager.AppSettings["auth0:Domain"])));
var token = client.ExchangeCodeForAccessTokenAsync(new ExchangeCodeRequest
{
ClientId = ConfigurationManager.AppSettings["auth0:ClientId"],
ClientSecret = ConfigurationManager.AppSettings["auth0:ClientSecret"],
AuthorizationCode = context.Request.QueryString["code"],
RedirectUri = context.Request.Url.ToString()
});
var profile = client.GetUserInfoAsync(token.Result.AccessToken);
var user = new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("name", profile.Result.UserName ?? profile.Result.Email),
new KeyValuePair<string, object>("email", profile.Result.Email),
new KeyValuePair<string, object>("family_name", profile.Result.LastName),
new KeyValuePair<string, object>("given_name", profile.Result.FirstName),
new KeyValuePair<string, object>("nickname", profile.Result.NickName),
new KeyValuePair<string, object>("picture", profile.Result.Picture),
new KeyValuePair<string, object>("user_id", profile.Result.UserId),
new KeyValuePair<string, object>("id_token", token.Result.IdToken),
new KeyValuePair<string, object>("access_token", token.Result.AccessToken),
new KeyValuePair<string, object>("refresh_token", token.Result.RefreshToken),
new KeyValuePair<string, object>("connection", profile.Result.Identities.First().Connection),
new KeyValuePair<string, object>("provider", profile.Result.Identities.First().Provider)
};
// NOTE: Uncomment the following code in order to include claims from associated identities
profile.Result.Identities.ToList().ForEach(i =>
{
user.Add(new KeyValuePair<string, object>(i.Connection + ".access_token", i.AccessToken));
user.Add(new KeyValuePair<string, object>(i.Connection + ".provider", i.Provider));
user.Add(new KeyValuePair<string, object>(i.Connection + ".user_id", i.UserId));
});
// NOTE: uncomment this if you send roles
user.Add(new KeyValuePair<string, object>(ClaimTypes.Role, profile.Result.ProviderAttributes["roles"]));
// NOTE: this will set a cookie with all the user claims that will be converted
// to a ClaimsPrincipal for each request using the SessionAuthenticationModule HttpModule.
// You can choose your own mechanism to keep the user authenticated (FormsAuthentication, Session, etc.)
FederatedAuthentication.SessionAuthenticationModule.CreateSessionCookie(user);
if (context.Request.QueryString["state"] != null && context.Request.QueryString["state"].StartsWith("ru="))
{
var state = HttpUtility.ParseQueryString(context.Request.QueryString["state"]);
context.Response.Redirect(state["ru"], true);
}
context.Response.Redirect("/");
}
}
我有类似的问题 (.NET 4.5),我的异步处理程序一直在工作,直到我将以下行添加到 Web.config
:
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="false" />
我不得不将所有 await
替换为 .Result
并实现非异步 IHttpHandler 接口,以使一切恢复正常。我无法从配置中删除设置,因为 Page.RegisterAsyncTask
需要它
也许对某人有帮助
我在我的应用程序中使用 auth0 进行身份验证、角色和权限。我正在按照下面的教程在我的应用程序中实现 auth0,
它为我创建了登录屏幕,在提供电子邮件 ID 和密码后,它导航到 LoginHandler.ashx。它显示在错误页面下方。
我正在获取令牌、用户 ID 和其他信息,但如何解决处理程序中的这个错误?
这是我的解决方案,如果有人遇到同样的问题,
public class LoginCallback : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
AuthenticationApiClient client = new AuthenticationApiClient(
new Uri(string.Format("https://{0}", ConfigurationManager.AppSettings["auth0:Domain"])));
var token = client.ExchangeCodeForAccessTokenAsync(new ExchangeCodeRequest
{
ClientId = ConfigurationManager.AppSettings["auth0:ClientId"],
ClientSecret = ConfigurationManager.AppSettings["auth0:ClientSecret"],
AuthorizationCode = context.Request.QueryString["code"],
RedirectUri = context.Request.Url.ToString()
});
var profile = client.GetUserInfoAsync(token.Result.AccessToken);
var user = new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("name", profile.Result.UserName ?? profile.Result.Email),
new KeyValuePair<string, object>("email", profile.Result.Email),
new KeyValuePair<string, object>("family_name", profile.Result.LastName),
new KeyValuePair<string, object>("given_name", profile.Result.FirstName),
new KeyValuePair<string, object>("nickname", profile.Result.NickName),
new KeyValuePair<string, object>("picture", profile.Result.Picture),
new KeyValuePair<string, object>("user_id", profile.Result.UserId),
new KeyValuePair<string, object>("id_token", token.Result.IdToken),
new KeyValuePair<string, object>("access_token", token.Result.AccessToken),
new KeyValuePair<string, object>("refresh_token", token.Result.RefreshToken),
new KeyValuePair<string, object>("connection", profile.Result.Identities.First().Connection),
new KeyValuePair<string, object>("provider", profile.Result.Identities.First().Provider)
};
// NOTE: Uncomment the following code in order to include claims from associated identities
profile.Result.Identities.ToList().ForEach(i =>
{
user.Add(new KeyValuePair<string, object>(i.Connection + ".access_token", i.AccessToken));
user.Add(new KeyValuePair<string, object>(i.Connection + ".provider", i.Provider));
user.Add(new KeyValuePair<string, object>(i.Connection + ".user_id", i.UserId));
});
// NOTE: uncomment this if you send roles
user.Add(new KeyValuePair<string, object>(ClaimTypes.Role, profile.Result.ProviderAttributes["roles"]));
// NOTE: this will set a cookie with all the user claims that will be converted
// to a ClaimsPrincipal for each request using the SessionAuthenticationModule HttpModule.
// You can choose your own mechanism to keep the user authenticated (FormsAuthentication, Session, etc.)
FederatedAuthentication.SessionAuthenticationModule.CreateSessionCookie(user);
if (context.Request.QueryString["state"] != null && context.Request.QueryString["state"].StartsWith("ru="))
{
var state = HttpUtility.ParseQueryString(context.Request.QueryString["state"]);
context.Response.Redirect(state["ru"], true);
}
context.Response.Redirect("/");
}
}
我有类似的问题 (.NET 4.5),我的异步处理程序一直在工作,直到我将以下行添加到 Web.config
:
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="false" />
我不得不将所有 await
替换为 .Result
并实现非异步 IHttpHandler 接口,以使一切恢复正常。我无法从配置中删除设置,因为 Page.RegisterAsyncTask
也许对某人有帮助