c# - ExternalLoginConfirmation returns null after facebook succesful login -


implementing facebook login in mvc 5 template,had added app id , secret code.

initially login failing returning null

public async task<actionresult> externallogincallback(string returnurl) {     // crashes on line     var logininfo = await authenticationmanager.getexternallogininfoasync();     if (logininfo == null)     {         return redirecttoaction("login");     } } 

after searching came across solution says replace existing externallogincallback method

[allowanonymous]     public async task<actionresult> externallogincallback(string returnurl)     {         var result = await authenticationmanager.authenticateasync(defaultauthenticationtypes.externalcookie);         if (result == null || result.identity == null)         {             return redirecttoaction("login");         }          var idclaim = result.identity.findfirst(claimtypes.nameidentifier);         if (idclaim == null)         {             return redirecttoaction("login");         }          var login = new userlogininfo(idclaim.issuer, idclaim.value);         var name = result.identity.name == null ? "" : result.identity.name.replace(" ", "");          // sign in user external login provider if user has login         var user = await usermanager.findasync(login);         if (user != null)         {             await signinasync(user, ispersistent: false);             return redirecttolocal(returnurl);         }         else         {             // if user not have account, prompt user create account             viewbag.returnurl = returnurl;             viewbag.loginprovider = login.loginprovider;             return view("externalloginconfirmation", new externalloginconfirmationviewmodel { username = name });         }     } 

now issue solved when try associate facebook account. you've authenticated facebook. please enter user name site below , click register button finish logging in.

[httppost]     [allowanonymous]     [validateantiforgerytoken]     public async task<actionresult> externalloginconfirmation(externalloginconfirmationviewmodel model, string returnurl)     {         if (user.identity.isauthenticated)         {             return redirecttoaction("manage");         }          if (modelstate.isvalid)         {             // here comes error system.nullreferenceexception: object reference not set instance of object.              var info = await authenticationmanager.getexternallogininfoasync();             if (info == null)             {                 return view("externalloginfailure");             }             var user = new applicationuser() { username = model.username };             var result = await usermanager.createasync(user);             if (result.succeeded)             {                 result = await usermanager.addloginasync(user.id, info.login);                 if (result.succeeded)                 {                     await signinasync(user, ispersistent: false);                     return redirecttolocal(returnurl);                 }             }             adderrors(result);         }          viewbag.returnurl = returnurl;         return view(model);     } 

on debugging username come can't figure out why throwing exception.

i think when gets getexternallogininfosync returns null.

i had same problem , here how managed fix , email facebook.

  • update following nuget pacakges
    • microsoft.owin version 3.1.0-rc1
    • microsoft.owin.security version 3.1.0-rc1
    • microsoft.owin.security.cookies version 3.1.0-rc1
    • microsoft.owin.security.oauth version 3.1.0-rc1
    • microsoft.owin.security.facebook version 3.1.0-rc1

then add following code identity startup class

var facebookoptions = new facebookauthenticationoptions()         {             appid = "your app id",             appsecret = "your app secret",             backchannelhttphandler = new facebookbackchannelhandler(),             userinformationendpoint = "https://graph.facebook.com/v2.8/me?fields=id,name,email,first_name,last_name",             scope = { "email" }         };          app.usefacebookauthentication(facebookoptions); 

this definition class facebookbackchannelhandler():

using system; using system.net.http;  public class facebookbackchannelhandler : httpclienthandler {     protected override async system.threading.tasks.task<httpresponsemessage> sendasync(         httprequestmessage request,         system.threading.cancellationtoken cancellationtoken)     {         // replace requesturi it's not malformed         if (!request.requesturi.absolutepath.contains("/oauth"))         {             request.requesturi = new uri(request.requesturi.absoluteuri.replace("?access_token", "&access_token"));         }          return await base.sendasync(request, cancellationtoken);     } } 

Comments