2017-07-17 16 views
0

私はTwitterやマイクロソフトのためにnullを返す下の行に問題が生じています:OwinのOAuthプロバイダーTwitterやマイクロソフト

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); 

これは以下のようにアカウントのコントローラである:スタートアップで

[AllowAnonymous] 
public async Task<ActionResult> ExternalLoginCallback(string returnUrl) 
{ 
    var loginInfo = await 
    AuthenticationManager.GetExternalLoginInfoAsync(); 

    if (loginInfo == null) 
    { 
     return RedirectToAction("Login"); 
    } 

    var result = await SignInManager.ExternalSignInAsync(loginInfo, false); 
    switch (result) 
    { 
     case SignInStatus.Success: 
      return RedirectToLocal(returnUrl); 
     case SignInStatus.LockedOut: 
      return View("Lockout"); 
     //case SignInStatus.RequiresVerification: 
     // return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false }); 
     case SignInStatus.Failure: 
     default: 
      // If the user does not have an account, then prompt the user to create an account 
      ViewBag.ReturnUrl = returnUrl; 
      ViewBag.LoginProvider = loginInfo.Login.LoginProvider; 
      return View("ExternalLoginConfirmation", new AccountExternalLoginConfirmationViewModel { Email = loginInfo.Email }); 
    } 
} 

.auth.cs現在の設定は次のとおりです。

app.UseTwitterAuthentication(
    new TwitterAuthenticationOptions() 
    { 
     ConsumerKey = ConfigurationManager.AppSettings["TwitterAPIKey"], 
     ConsumerSecret = ConfigurationManager.AppSettings["TwitterAPISecret"], 

     Provider = new TwitterAuthenticationProvider() 
     { 
      OnAuthenticated = context => 
      { 
       context.Identity.AddClaim(new Claim("urn:tokens:twitter:accesstoken", context.AccessToken)); 
       context.Identity.AddClaim(new Claim("urn:tokens:twitter:accesstokensecret", 
        context.AccessTokenSecret)); 
       return Task.FromResult(true); 
      } 
     } 
    }); 

     app.UseMicrosoftAccountAuthentication(new MicrosoftAccountAuthenticationOptions() 
     { 
      ClientId = ConfigurationManager.AppSettings["MicrosoftAPIKey"], 
      ClientSecret = ConfigurationManager.AppSettings["MicrosoftAPISecret"], 
      // Scope = { "wl.basic", "wl.emails" }, 
      Provider = new MicrosoftAccountAuthenticationProvider() 
      { 
       OnAuthenticated = context => 
       { 
        context.Identity.AddClaim(new Claim("urn:microsoftaccount:access_token", context.AccessToken, "Microsoft")); 
        context.Identity.AddClaim(new Claim("urn:microsoft:email", context.Email)); 
        return Task.FromResult(true); 
       } 
      } 
     }); 

Scope = { "wl.basic"、 "wl.emails"}をMicrosoftAccountAuthenticationOptionsに追加します。しかし、これは悪い要求を返します。 twitterとmicrosoft loginでこの問題を解決する方法に関するアイデア。私はマイクロソフトのために使用しています

私のURLが リダイレクトURLです:https://localhost/signin-microsoft ログアウトURL:https://localhost/account/logout ホームページ:https://localhost

Twitterの ウェブサイト:https://127.0.0.1 コールバックURL:https://127.0.0.1/signin-twitter

私はライブで試してみましたまたライブにURLがあり、まだヌルになっています var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

+0

こんにちは、あなたはこの問題の解決策を見つけましたか?私も同じ問題を抱えていますが、私が見つけた解決策はありません。 –

答えて

0

これを試してみてください:

var options = new TwitterAuthenticationOptions { SignInAsAuthenticationType = signInAsType, ConsumerKey = "...", ConsumerSecret = "...", Provider = new TwitterAuthenticationProvider() { OnAuthenticated = async ctx => { var manager = new OAuth.Manager( "-your-twitter-access-token-", "-your-twitter-access-token-secret-", ctx.AccessToken, ctx.AccessTokenSecret); var url = "https://api.twitter.com/1.1/account/verify_credentials.json"; var authzHeader = manager.GenerateAuthzHeader(url, "GET"); var request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.PreAuthenticate = true; request.AllowWriteStreamBuffering = true; request.Headers.Add("Authorization", authzHeader); using (var response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) throw new Exception("NOK"); var responseStream = response.GetResponseStream(); var reader = new System.IO.StreamReader(responseStream); var res = reader.ReadToEnd(); Newtonsoft.Json.Linq.JObject data = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(res); var claims = new List<Claim>(); claims.Add(new Claim(Core.Constants.ClaimTypes.RawData, ctx.Identity.Claims.ToJsonString())); claims.Add(new Claim(Core.Constants.ClaimTypes.AccessToken, ctx.AccessToken)); claims.Add(new Claim(Core.Constants.ClaimTypes.AccessTokenSecret, ctx.AccessTokenSecret)); claims.Add(new Claim(Core.Constants.ClaimTypes.Subject, ctx.UserId)); claims.Add(new Claim(Core.Constants.ClaimTypes.Name, data["name"].TokenString())); claims.Add(new Claim(Core.Constants.ClaimTypes.Locale, GenerateLocale(data["lang"].TokenString()))); claims.Add(new Claim(Core.Constants.ClaimTypes.ZoneInfo, GenerateZone(data["location"].TokenString(), data["time_zone"].TokenString()))); claims.Add(new Claim(Core.Constants.ClaimTypes.WebSite, data["url"].TokenString())); claims.Add(new Claim(Core.Constants.ClaimTypes.ProfileUrl, "https://twitter.com/" + ctx.ScreenName)); claims.Add(new Claim(Core.Constants.ClaimTypes.Picture, data["profile_image_url"].TokenString())); await PrepClaims(ctx.Identity, claims); } } }