mvc中DotNetOpenAuth实现了第三方应用访问自己的网站

以yahoo为例吧,即从yahoo取得用户信息,存到自己的站点,实现了用户信息在一次录入多处共享的功能。
以下是在点击了使用yahoo登录本站的链接后执行action:OpenId.
ProviderUrl="http://yahoo.com"
Action OpenId会被执行两次,一次是自己的站点请求的,此时response==null;一次是yahoo请求的
,response!=null.


using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.OpenId.Provider;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using DotNetOpenAuth.Messaging;


static private readonly OpenIdRelyingParty Openid = new OpenIdRelyingParty();//一个重要的类
public ActionResult OpenId(string ProviderUrl) { FormsAuthentication auth = new FormsAuthentication(); var response = Openid.GetResponse(); if (response == null)//自己站点的请求 { Identifier id; if (Identifier.TryParse(ProviderUrl, out id))//第三方网站地址能产生一个有效标识符时 { var request = Openid.CreateRequest(id); var fetch = new FetchRequest();//定义在这次请求中都想要获取到用户的哪些信息 fetch.Attributes.AddRequired(WellKnownAttributes.Name.First); fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last); fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email); request.AddExtension(fetch); return request.RedirectingResponse.AsActionResult();//页面跳转到了yahoo } return RedirectToAction("Index"); } switch (response.Status)//yahoo登录后,返回到自己的站点时,根据不用的response status来做不同的处理 { case AuthenticationStatus.Authenticated: var fetch = response.GetExtension<FetchResponse>(); string firstName = string.Empty; string lastName = string.Empty; string email = string.Empty; if (fetch != null) { firstName = fetch.GetAttributeValue(WellKnownAttributes.Name.First); lastName = fetch.GetAttributeValue(WellKnownAttributes.Name.Last); email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email); }

           return CreateUser(response.ClaimedIdentifier, firstName, lastName, email);//可以将用户的信息存入到自己的数据库 case AuthenticationStatus.Failed: return RedirectToAction("Index"); default: return RedirectToAction("Index"); } }

 dotNetOpenAuth也可以实现sso,只是觉得晦涩难懂。比如:

<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS
    xmlns:xrds="xri://$xrds"
    xmlns:openid="http://openid.net/xmlns/1.0"
    xmlns="xri://$xrd*($v*2.0)">
    <XRD>
        <Service priority="10">
            <Type>http://specs.openid.net/auth/2.0/signon</Type>
            <Type>http://openid.net/extensions/sreg/1.1</Type>
            <URI>@uri1</URI>
        </Service>
        <Service priority="20">
            <Type>http://openid.net/signon/1.0</Type>
            <Type>http://openid.net/extensions/sreg/1.1</Type>
            <URI>@uri2</URI>
        </Service>
    </XRD>
</xrds:XRDS>

这里边具体都是做什么用呢,也找不到资料,觉得用dotnetopenauth实现sso可能有点自找麻烦的感觉。有对这块熟悉的吗?

 

 

posted @ 2014-03-14 14:42  被爱浸润的智慧体  阅读(654)  评论(0编辑  收藏  举报