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

asp.net-mvc-4 – MVC 4中客户URL的自定义OpenIdClient

发布时间:2020-12-16 07:32:21 所属栏目:asp.Net 来源:网络整理
导读:我正在使用MVC 4的默认模板,并尝试将我自己的openID提供程序(例如 http://steamcommunity.com/dev)添加到openID登录列表和openID框中,用户可以在其中键入其openID信息. 要添加Google,我只是取消评论 OAuthWebSecurity.RegisterGoogleClient(); 至于其他自定
我正在使用MVC 4的默认模板,并尝试将我自己的openID提供程序(例如 http://steamcommunity.com/dev)添加到openID登录列表和openID框中,用户可以在其中键入其openID信息.

要添加Google,我只是取消评论

OAuthWebSecurity.RegisterGoogleClient();

至于其他自定义解决方案,你可以做类似的事情

OAuthWebSecurity.RegisterClient(new SteamClient(),“Steam”,null);

我遇到的麻烦是创建SteamClient(或通用的)http://blogs.msdn.com/b/webdev/archive/2012/08/23/plugging-custom-oauth-openid-providers.aspx没有显示任何地方更改URL.

解决方法

根据@ Jeff的回答,我创建了一个类来处理Stack Exchange OpenID.

寄存器:

OAuthWebSecurity.RegisterClient(new StackExchangeOpenID());

类:

public class StackExchangeOpenID : OpenIdClient
{
    public StackExchangeOpenID()
        : base("stackexchange","https://openid.stackexchange.com")
    {

    }

    protected override Dictionary<string,string> GetExtraData(IAuthenticationResponse response)
    {
        FetchResponse fetchResponse = response.GetExtension<FetchResponse>();

        if (fetchResponse != null)
        {
            var extraData = new Dictionary<string,string>();
            extraData.Add("email",fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email));
            extraData.Add("name",fetchResponse.GetAttributeValue(WellKnownAttributes.Name.FullName));
            return extraData;
        }

        return null;
    }
    protected override void OnBeforeSendingAuthenticationRequest(IAuthenticationRequest request)
    {
        var fetchRequest = new FetchRequest();
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.FullName);
        request.AddExtension(fetchRequest);
    }
}

检索额外数据:

var result = OAuthWebSecurity.VerifyAuthentication();
result.ExtraData["email"];
result.ExtraData["name"];

(编辑:李大同)

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

    推荐文章
      热点阅读