外部登录时IdentityServer WithCustomUserService抛出空引用异常

时间:2016-10-25 10:05:29

标签: identityserver3

我实现了一个自定义用户服务来将用户数据存储在数据库中。

因为当我尝试使用像facebook这样的外部提供程序进行身份验证时,我得到了NullReferenceException。

我可以在异常中看到此堆栈,表明登录页面缺少值。结果在点击facebook按钮后,我再次站在起始登录页面。

enter image description here

但我不知道为什么或哪个值正好

我可以看到,在AuthenticateExternal context.AuthenticateResult.User.Claims的末尾包含这些声明

enter image description here

我的用户服务看起来像这样简化

 public override async Task AuthenticateExternalAsync(ExternalAuthenticationContext context)
{
     string id = context.ExternalIdentity.Claims.FirstOrDefault(i => i.Type == "id").Value;

                var user = await gateway.ByExternalIds(context.ExternalIdentity.Provider, id);

                if (user == null)
                {
                    string displayName = context.ExternalIdentity.Claims.FirstOrDefault(i => i.Type.Equals("urn:facebook:name")).Value;

                    user = new User(context.ExternalIdentity);

                    await gateway.StoreAsync(user);
                }

                if (user != null)

                {
                    await gateway.SetLastLogin(user.Subject, DateTimeOffset.Now);

                    context.AuthenticateResult = new AuthenticateResult(user.Subject, GetDisplayName(user), identityProvider: context.ExternalIdentity.Provider);
                }
}

我错过了什么?

1 个答案:

答案 0 :(得分:0)

找到它。如果用户处于活动状态时检测器的方法返回false,则会发生此行为。

Task IsActiveAsync(IsActiveContext context)

它有点意外,因为我曾预料到在这种情况下我会看到类似“未知用户”或“非活动用户”的内容