locked
Login with external providers returns the "object reference not set to an object instance" error RRS feed

  • Question

  • User-1352156089 posted

    Hi All,

    I have just tried to use the template in VW 2013 as shown in the following video:

    http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/oauth-in-the-default-aspnet-45-templates

    I have followed this video step by step but when I deploy my application to enable the login via Facebook, the application returns the following error:

    Do you have any tip about it?

    There is no much documentation in the Internet and most of it refers to MVC applications while I am using webforms.

    Thank you,

    Line 36:         If Not IsPostBack Then
    Line 37:             Dim manager = New UserManager()
    Line 38:             Dim loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo()
    Line 39:             If loginInfo Is Nothing Then
    Line 40:                 Response.Redirect("~/Account/Login")

    [NullReferenceException: Object reference not set to an object instance.
       Microsoft.Owin.Security.AuthenticationManagerExtensions.GetExternalLoginInfo(AuthenticateResult result) +81
       Microsoft.Owin.Security.<GetExternalLoginInfoAsync>d__7.MoveNext() +193
       System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
       System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
       Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func`1 func) +159
       Microsoft.Owin.Security.AuthenticationManagerExtensions.GetExternalLoginInfo(IAuthenticationManager manager) +71
       HPLogNew.RegisterExternalLogin.Page_Load() in C:\Users\Claudio\Desktop\HPLogNew\HPLogNew\HPLogNew\Account\RegisterExternalLogin.aspx.vb:38
       HPLogNew.RegisterExternalLogin._Lambda$__3(Object a0, EventArgs a1) in C:\Users\Claudio\Desktop\HPLogNew\HPLogNew\HPLogNew\Account\RegisterExternalLogin.aspx.designer.vb:0
       System.Web.UI.Control.OnLoad(EventArgs e) +92
       System.Web.UI.<LoadRecursiveAsync>d__4.MoveNext() +228
       System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
       System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
       System.Web.Util.WithinCancellableCallbackTaskAwaiter.GetResult() +30
       System.Web.UI.<ProcessRequestMainAsync>d__14.MoveNext() +4346D
    Saturday, August 2, 2014 11:33 AM

Answers

  • User-1352156089 posted

    Hi All,

    I solved the problem by simply updating all packages.

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 10, 2014 7:04 AM

All replies

  • User-1352156089 posted

    The problem seems to be related to the following line of code:

     Dim loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo()

    THis is really strange because I have used the Visual Studio 2013 template website. 

    What kind of changed should I apply to the code in order to make it work?

    Thank

    Sunday, August 3, 2014 12:45 PM
  • User-166373564 posted

    Hi Claudio,

    The problem seems to be related to the following line of code:

     Dim loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo()

    For this issue, I don't think there is something wrong with code above.

    Here is sample about RegisterExternalLogin:

    protected void Page_Load()
            {
                // Process the result from an auth provider in the request
                ProviderName = IdentityHelper.GetProviderNameFromRequest(Request);
                if (String.IsNullOrEmpty(ProviderName))
                {
                    RedirectOnFail();
                    return;
                }
                if (!IsPostBack)
                {
                    var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                    var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
                    if (loginInfo == null)
                    {
                        RedirectOnFail();
                        return;
                    }
                    var user = manager.Find(loginInfo.Login);
                    if (user != null)
                    {
                        IdentityHelper.SignIn(manager, user, isPersistent: false);
                        IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                    }
                    else if (User.Identity.IsAuthenticated)
                    {
                        // Apply Xsrf check when linking
                        var verifiedloginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo(IdentityHelper.XsrfKey, User.Identity.GetUserId());
                        if (verifiedloginInfo == null)
                        {
                            RedirectOnFail();
                            return;
                        }
     
                        var result = manager.AddLogin(User.Identity.GetUserId(), verifiedloginInfo.Login);
                        if (result.Succeeded)
                        {
                            IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                        }
                        else
                        {
                            AddErrors(result);
                            return;
                        }
                    }
                    else
                    {
                        email.Text = loginInfo.Email;
                    }
                }
            }        
    

    Hope it helps.

    Regards

    Monday, August 4, 2014 3:20 AM
  • User-1352156089 posted

    Thank you Angie,

    I translated your code into VB:

    Protected Sub Page_Load()
    	' Process the result from an auth provider in the request
    	ProviderName = IdentityHelper.GetProviderNameFromRequest(Request)
    	If [String].IsNullOrEmpty(ProviderName) Then
    		RedirectOnFail()
    		Return
    	End If
    	If Not IsPostBack Then
    		Dim manager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)()
    		Dim loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo()
    		If loginInfo Is Nothing Then
    			RedirectOnFail()
    			Return
    		End If
    		Dim user__1 = manager.Find(loginInfo.Login)
    		If user__1 IsNot Nothing Then
    			IdentityHelper.SignIn(manager, user__1, isPersistent := False)
    			IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response)
    		ElseIf User.Identity.IsAuthenticated Then
    			' Apply Xsrf check when linking
    			Dim verifiedloginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo(IdentityHelper.XsrfKey, User.Identity.GetUserId())
    			If verifiedloginInfo Is Nothing Then
    				RedirectOnFail()
    				Return
    			End If
    
    			Dim result = manager.AddLogin(User.Identity.GetUserId(), verifiedloginInfo.Login)
    			If result.Succeeded Then
    				IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response)
    			Else
    				AddErrors(result)
    				Return
    			End If
    		Else
    			email.Text = loginInfo.Email
    		End If
    	End If
    End Sub

    and it gives me lots of errors.

    The above doesnt seem to work.

    My entire code I am using is:

    Imports Microsoft.AspNet.Identity
    Imports Microsoft.AspNet.Identity.EntityFramework
    Imports Microsoft.AspNet.Identity.Owin
    Imports System.Security.Claims
    Imports System.Web
    Imports Microsoft.Owin.Security
    
    Public Partial Class Account_RegisterExternalLogin
        Inherits System.Web.UI.Page
        Protected Property ProviderName() As String
            Get
                Return If(DirectCast(ViewState("ProviderName"), String), [String].Empty)
            End Get
            Private Set(value As String)
                ViewState("ProviderName") = value
            End Set
        End Property
    
        Protected Property ProviderAccountKey() As String
            Get
                Return If(DirectCast(ViewState("ProviderAccountKey"), String), [String].Empty)
            End Get
            Private Set(value As String)
                ViewState("ProviderAccountKey") = value
            End Set
        End Property
    
        Protected Sub Page_Load() Handles Me.Load
            ' Elaborare il risultato fornito da un provider di autenticazione nella richiesta
            ProviderName = IdentityHelper.GetProviderNameFromRequest(Request)
            If [String].IsNullOrEmpty(ProviderName) Then
                Response.Redirect("~/Account/Login")
            End If
    
            If Not IsPostBack Then
                Dim manager = New UserManager()
                Dim loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo()
                If loginInfo Is Nothing Then
                    Response.Redirect("~/Account/Login")
                End If
                Dim appuser = manager.Find(loginInfo.Login)
                If appuser IsNot Nothing Then
                    IdentityHelper.SignIn(manager, appuser, isPersistent:=False)
                    IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response)
                ElseIf User.Identity.IsAuthenticated Then
                    Dim verifiedloginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo(IdentityHelper.XsrfKey, User.Identity.GetUserId())
                    If verifiedloginInfo Is Nothing Then
                        Response.Redirect("~/Account/Login")
                    End If
    
                    Dim result = manager.AddLogin(User.Identity.GetUserId(), verifiedloginInfo.Login)
                    If result.Succeeded Then
                        IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response)
                    Else
                        AddErrors(result)
                        Return
                    End If
                Else
                    userName.Text = loginInfo.DefaultUserName
                End If
            End If
        End Sub
    
        Protected Sub LogIn_Click(sender As Object, e As EventArgs)
            CreateAndLoginUser()
        End Sub
    
        Private Sub CreateAndLoginUser()
            If Not IsValid Then
                Return
            End If
            Dim manager = New UserManager()
            Dim user = New ApplicationUser() With {.UserName = userName.Text}
            Dim result = manager.Create(user)
            If Not result.Succeeded Then
                AddErrors(result)
                Return
            End If
            Dim loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo()
            If loginInfo Is Nothing Then
                Response.Redirect("~/Account/Login")
                Return
            End If
            result = manager.AddLogin(user.Id, loginInfo.Login)
            If Not result.Succeeded Then
                AddErrors(result)
                Return
            End If
            IdentityHelper.SignIn(manager, user, False)
            IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response)
            Return
        End Sub
    
        Private Sub AddErrors(result As IdentityResult)
            For Each [error] As String In result.Errors
                ModelState.AddModelError("", [error])
            Next
        End Sub
    End Class

    Thanks

    Tuesday, August 5, 2014 1:44 PM
  • User-1352156089 posted

    Hi All,

    I solved the problem by simply updating all packages.

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 10, 2014 7:04 AM