Google账户登录服务
生活随笔
收集整理的這篇文章主要介紹了
Google账户登录服务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
現在有些網站有了Google賬戶登錄的功能,如下圖,這樣的登錄方式其實對用戶是有益的。只要我有個google賬號,就能登錄有利用Google這個登錄服務的網站。此功能詳解請參考
此功能的流程圖:
?
描述:
以www.sopif.com為例
http://www.sopif.com/logintest.aspx這個頁面,里面有個按鈕,按鈕對應的事件代碼
Codestring?s?=?"https://www.google.com/accounts/AuthSubRequest?next=http%3A%2F%2Fwww.sopif.com%2Flhking.aspx&scope=http%3A%2F%2Fwww.google.com%2Fcalendar%2Ffeeds%2F&session=1&secure=1";
????????Response.Redirect(s);
next參數,就是google登錄成功后,重定向的頁面,scope就是你要訪問的google服務。
?
我的next參數頁面時lhking.aspx
頁面代碼:
Code?object?token;
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????token?=?Request.QueryString["token"];
????????if?(token?!=?null)
????????{
????????????Response.Write("你用Google賬戶登錄成功!"+"<br?/>");
????????????Response.Write(token.ToString());
????????}
????????else
????????{
????????????Response.Write("你用Google賬戶登錄失敗!");
????????}
????}
?
這樣就行了。大家可以測試,這種情況下,google只會返回一個token,用戶名和密碼都是安全的。
要想獲得google返回來的郵箱地址和姓名:
請參看一下類,用的時候,直接 Response.Redirect(new GoogleLogin().getLoginUrl()); //Google賬戶登錄
Code?///?<summary>
????///?提供google賬戶的登錄
????///?</summary>
????public?class?GoogleLogin?:?ILoginService
????{
????????string?urlAuthSubRequest?=?"https://www.google.com/accounts/AuthSubRequest?";
????????string?next?=?ILoginService.WebsiteUrl()?+?"/Auth/Handle.aspx";
????????string?scope?=?"http://www.google.com/m8/feeds/contacts/default/thin?max-results=0";
????????string?urldata?=?"";
????????///?<summary>
????????///?生成登錄用的url
????????///?</summary>
????????///?<returns></returns>
????????public?override?string?getLoginUrl()
????????{
????????????return?urlAuthSubRequest?+?"next="?+?HttpUtility.UrlEncode(next)?+?"&scope="?+?HttpUtility.UrlEncode(scope);
????????}
????????///?<summary>
????????///?得到Google賬戶的Email地址和姓名
????????///?</summary>
????????///?<param?name="page"></param>
????????///?<returns></returns>
????????public?override?string[]?parseHandle(HttpContext?page)
????????{
????????????urldata?=?scope;
????????????string[]?ret?=?new?string[2];
????????????try
????????????{
????????????????string?token?=?page.Request["token"];
????????????????HttpWebRequest?request?=?(HttpWebRequest)WebRequest.Create(new?Uri(urldata));
????????????????request.Headers.Add("Authorization",?"AuthSub?token="?+?token);
????????????????request.Method?=?"GET";
????????????????HttpWebResponse?response?=?(HttpWebResponse)request.GetResponse();
????????????????XmlDocument?doc?=?new?XmlDocument();
????????????????if?(response?!=?null)
????????????????{
????????????????????string?account?=?string.Empty,?name?=?string.Empty;
????????????????????doc.Load(response.GetResponseStream());
????????????????????XmlNode?node?=?doc.SelectSingleNode("*/*[local-name()='id']");
????????????????????if?(node?!=?null)
????????????????????{
????????????????????????account?=?node.InnerText;??//Email地址
????????????????????}
????????????????????node?=?doc.SelectSingleNode("*/*[local-name()='author']/*[local-name()='name']");
????????????????????if?(node?!=?null)
????????????????????{
????????????????????????name?=?node.InnerText;?//姓名
????????????????????}
????????????????????ret[0]?=?account;
????????????????????ret[1]?=?name;
????????????????????return?ret;
????????????????}
????????????????else?return?ret;
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????new?MyException("GoogleLogin",?ex.ToString());
????????????????return?new?string[]?{?"",?""?};
????????????}
????????}
????}
轉載于:https://www.cnblogs.com/lhking/archive/2009/10/04/1578058.html
總結
以上是生活随笔為你收集整理的Google账户登录服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AWS启示录:创新作帆,云计算的征途是汪
- 下一篇: 服务器无法显示url,遇到“您所请求的网