2016-12-23 6 views
1

私のアプリケーションで認証、ロール、およびアクセス許可にauth0を使用しています。私はそれがLoginHandler.ashxに移動し、電子メールIDとパスワードを与えた後に、それは私のためのログイン画面を作成しauth0認証の問題:HttpContext.Currentがnullです。このコードパスは、ASP.NETの実行コンテキストでのみ有効です

https://auth0.com/docs/quickstart/webapp/aspnet

、自分のアプリケーションにauth0を実装するためのチュートリアルの下に次のです。以下のエラーページが表示されます。

enter image description here

私はハンドラでこのエラーを解決するために、トークン、ユーザIDおよびその他の情報が、どのように取得していますか?誰もが同じ問題に直面した場合はここで

+0

は、あなたがこのコード(IIS、IIS Expressを)ホスティングしている方法についての詳細な情報を提供することはできますか?どの.NET Frameworkのバージョンをターゲットにしていますか? –

+0

私はVisual Studio 2015、Framework 4.6を使用しています。私はリモートシステムに取り組んでいます。 –

+0

ホスティング用IIS Expressを使用しています –

答えて

2

は、私の解決策である、

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("/"); 

      } 
} 
関連する問題