Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 找不到用户ID_C#_Asp.net_.net_Asp.net Mvc_Linkedin - Fatal编程技术网

C# 找不到用户ID

C# 找不到用户ID,c#,asp.net,.net,asp.net-mvc,linkedin,C#,Asp.net,.net,Asp.net Mvc,Linkedin,我真的刚刚开始接触这个LinkedIn API以及ASP.NET MVC,所以请耐心听我说。我正在尝试对我的用户进行身份验证,这似乎正在工作,但当我尝试存储accessToken值(该值似乎也是有效的)时,我收到了错误: 异常详细信息:System.InvalidOperationException:找不到用户ID 错误发生在注释处//错误发生在此处 public async Task<ActionResult> ExternalLoginCallback(string return

我真的刚刚开始接触这个LinkedIn API以及ASP.NET MVC,所以请耐心听我说。我正在尝试对我的用户进行身份验证,这似乎正在工作,但当我尝试存储accessToken值(该值似乎也是有效的)时,我收到了错误:

异常详细信息:System.InvalidOperationException:找不到用户ID

错误发生在注释处//错误发生在此处

public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            return RedirectToAction("Login");
        }

        var claimsIdentity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);

        if (claimsIdentity != null)
        {
            var userIdClaim = claimsIdentity.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);

            if (userIdClaim != null)
            {
                // userIdClaim.Value here is: 5-0Vfh4Gv_
                var accessToken = claimsIdentity.FindAll(loginInfo.Login.LoginProvider + "_AccessToken").First();

                if (accessToken != null)
                {
                    // gets to this point, but...
                    await UserManager.AddClaimAsync(userIdClaim.Value, accessToken); // ERROR OCCURS HERE
                }
            }
        }

... ... ... 
}
此代码99%来自VisualStudio中的MVC应用程序模板。我唯一更改的是添加linkedin NuGet包安装linkedin包,并在Startup.Auth.cs中设置API密钥/密码。这完全有可能,即使我只是做了一些不正常的事情,或者获取了错误的用户ID

我已经看过了我能找到的每一个例子和视频,但仍然无法理解这一点

有人能帮我处理这个用户错误消息吗?还有,我只是缺少一些一般的最佳实践之类的东西吗?感到失落和沮丧


谢谢大家!

错误消息说明了一切。我认为您没有正确获取用户Id

试一试

而不是

await UserManager.AddClaimAsync(userIdClaim.Value, accessToken);

问题是在这一点上:

await UserManager.AddClaimAsync(userIdClaim.Value, accessToken);
第一个参数应该是标识数据库表中用户的UserId,即aspnetusers

您正在从LinkedIn传入nameidentifer,因此,您的数据库中不存在该用户


我认为您需要首先检查外部用户是否已注册,如果未注册,则创建帐户,添加外部登录名,然后添加索赔。

AddClaimesync采用了哪些参数。我能想到的原因是它可能接受了一些对象和对象值,并且您已经将这些变量定义为var,它不能隐式转换回用户定义的类类型GetUserId返回null,而且userIDclaim.Value确实有一个合法的用户ID——至少我认为它是合法的——请参见代码中的注释——看起来像我以前见过的其他用户。知道函数为什么会返回null吗?在这一步之前,我是否遗漏了填充该值的内容?请参阅@Brendan Green answer。在确认外部登录成功后,首先需要在标识表中创建用户,然后在添加声明时使用标识表中分配的用户Id。用户需要一个本地身份AFAIK。看见
await UserManager.AddClaimAsync(userIdClaim.Value, accessToken);