加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – MVC 4 OAuth集成.接下来是什么以及如何从数据提

发布时间:2020-12-16 07:12:21 所属栏目:asp.Net 来源:网络整理
导读:我发现了很多帖子和文章,其中包含有关如何配置MVC 4应用程序以与任何社交网络提供商集成以及如何对用户进行身份验证的非常详细的信息,但下一步是什么?例如,如何获取有关经过身份验证的用户的任何信息?最简单的任务是如何获取有关经过身份验证的用户的一些
我发现了很多帖子和文章,其中包含有关如何配置MVC 4应用程序以与任何社交网络提供商集成以及如何对用户进行身份验证的非常详细的信息,但下一步是什么?例如,如何获取有关经过身份验证的用户的任何信息?最简单的任务是如何获取有关经过身份验证的用户的一些信息 – 名字,姓氏,头像的网址,朋友列表等?

更新:

>这里有一些分享一些亮点的post和article
>有用article如何与Facebook互动

解决方法

OAuth仅用于验证,即获取访问令牌.获得此访问令牌后,您可以使用它从服务提供商处检索此信息.请查阅提供商的文档,了解如何完成此操作.

您可能会检索一些声明,例如FirstName和LastName,因为它们是标准的,大多数提供程序都支持它们.例如,在ExternalLoginCallback回调中,您可以尝试从result.ExtraData字典中检索此信息:

[AllowAnonymous]
public ActionResult ExternalLoginCallback(string returnUrl)
{
    AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback",new { ReturnUrl = returnUrl }));
    if (!result.IsSuccessful)
    {
        return RedirectToAction("ExternalLoginFailure");
    }

    if (OAuthWebSecurity.Login(result.Provider,result.ProviderUserId,createPersistentCookie: false))
    {
        return RedirectToLocal(returnUrl);
    }

    if (User.Identity.IsAuthenticated)
    {
        // Here you could use result.ExtraData dictionary
        string name = result.ExtraData["name"];


        // If the current user is logged in add the new account
        OAuthWebSecurity.CreateOrUpdateAccount(result.Provider,User.Identity.Name);
        return RedirectToLocal(returnUrl);
    }
    else
    {
        // User is new,ask for their desired membership name
        string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider,result.ProviderUserId);
        ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName;
        ViewBag.ReturnUrl = returnUrl;
        return View("ExternalLoginConfirmation",new RegisterExternalLoginModel { UserName = result.UserName,ExternalLoginData = loginData });
    }
}

但是不同的提供商可能使用不同的密钥.因此,根据使用的提供程序,您必须使用正确的密钥来读取所需的信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读