日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Google账户登录服务

發布時間:2023/12/8 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Google账户登录服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

現在有些網站有了Google賬戶登錄的功能,如下圖,這樣的登錄方式其實對用戶是有益的。只要我有個google賬號,就能登錄有利用Google這個登錄服務的網站。此功能詳解請參考

此功能的流程圖:

?

描述:

以www.sopif.com為例

http://www.sopif.com/logintest.aspx這個頁面,里面有個按鈕,按鈕對應的事件代碼

Code
string?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账户登录服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。