DotNetOpenAuth响应对象&属性已设置但它会抛出NullException

时间:2012-08-19 08:42:39

标签: c# asp.net asp.net-mvc openid dotnetopenauth

原来如此!我的脑袋爆炸了,因为这对我来说似乎很奇怪。使用调试模式,我可以看到Claimsresponse进入方法,填充它,以及Email属性。但是,当我尝试一个简单的字符串myStr = response.Email;它抛出NullException错误......

我不明白..

代码:(设置电子邮件失败!)

public static bool LoginUserUsingOpenID(BaseDBContext db, ClaimsResponse r)
            {
                string email = r.Email;
                bool doesntExist = !UserHelper.DoesUserExist(db, r.Email);

                User u = null;

                if (doesntExist)
                {
                    u.Email = email;
                    u.Username = "User" + new Random().Next(100000);
                    u.Password = CreateRandomPassword(7);
                    u.prepareForCreationFromThirdParty();
                }
                else
                {
                    u = db.Users.Where(x => x.Email == r.Email).FirstOrDefault();
                }
                SetUserAsAuthenticated(u);
                AddAutomaticLoginKeyForUser(db, u); //Need to get this to the users frontend somehow..
                return true;
            }

这是堆栈跟踪:

System.NullReferenceException: Object reference not set to an instance of an object.
   at MusingMonkey.Helpers.SecurityHelper.LoginUserUsingOpenID(BaseDBContext db, ClaimsResponse r) in C:\Users\William-Business\Desktop\TWB\TWB Central\Projects\MusingMonkey\MusingMonkey\Helpers\SecurityHelper.cs:line 170
   at MusingMonkey.Controllers.UsersController.HandleOpenIDResponse() in C:\Users\William-Business\Desktop\TWB\TWB Central\Projects\MusingMonkey\MusingMonkey\Controllers\UsersController.cs:line 78
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)

1 个答案:

答案 0 :(得分:1)

<呃嗯,你确定那是投掷NRE的r.Email行吗?

因为烟雾似乎来自以下行:

User u = null;

然后您尝试访问u实例而不必实例化此变量:

if (doesntExist)
{
    u.Email = email;
    ...

所以你可能意味着别的东西,例如:

User u = new User();