zoukankan      html  css  js  c++  java
  • 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可能有点自找麻烦的感觉。有对这块熟悉的吗?

  • 相关阅读:
    左侧导航太长了?
    组织结构配置文件的诡异行为
    SharePoint上无法显示Lync的在线状态
    多语言和自定义CSS
    文档库下载副本,文件名被截断
    在线menu生成网站
    FC7崩溃,安装Ubuntu,相当不错的系统
    简易下载baidu音乐排行榜音乐
    java放射调用静态方法和构造函数
    升级到Ubuntu7.10导致Eclipse中键盘无效的问题
  • 原文地址:https://www.cnblogs.com/Gift/p/3598835.html
Copyright © 2011-2022 走看看