MVC 5 Owin Facebook Auth results in Null Reference Exception

后端 未结 11 1146
忘了有多久
忘了有多久 2020-12-01 11:52

I\'m trying to setup integrated OWIN Facebook authentication in a new MVC 5 project in Visual Studio 2013. I have configured apps and keys as per this tutorial:

http

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 12:57

    Hongye Sun did all the heavy lifting in his answer above.

    Here's some code that can be added to your controller class and be called in place of the troublesome AuthenticationManager.GetExternalLoginInfoAsync().

    private async Task AuthenticationManager_GetExternalLoginInfoAsync_Workaround()
    {
        ExternalLoginInfo loginInfo = null;
    
        var result = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie);
    
        if (result != null && result.Identity != null)
        {
            var idClaim = result.Identity.FindFirst(ClaimTypes.NameIdentifier);
            if (idClaim != null)
            {
                loginInfo = new ExternalLoginInfo()
                {
                    DefaultUserName = result.Identity.Name == null ? "" : result.Identity.Name.Replace(" ", ""),
                    Login = new UserLoginInfo(idClaim.Issuer, idClaim.Value)
                };
            }
        }
        return loginInfo;
    }
    

提交回复
热议问题