0
votes

I am having issues with the below line returning null for twitter and microsoft:

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

this is in the account controller like below:

[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 });
    }
}

In the startup.auth.cs the current configuration is:

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);
               }
           }
        });

It has been suggested including Scope = { "wl.basic", "wl.emails" } in MicrosoftAccountAuthenticationOptions. This returns a bad request however. Any ideas on the way to resolve this issue with twitter and microsoft login.

My urls I am using for microsoft are Redirect Url: https://localhost/signin-microsoft Logout Url: https://localhost/account/logout Homepage: https://localhost

Twitter Website: https://127.0.0.1 Call Back url: https://127.0.0.1/signin-twitter

I have tried with live urls on live also and am still getting null on var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

1
Hi, have you found a solution for this problem? I am having the same issue, but none of the solutions I've found works. - Franz Kiermaier

1 Answers

0
votes

Try this:



  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);

                    }
                }
            }