Google Account Logon Service

Source: Internet
Author: User

Some websites now have the Google account logon function. For example, such logon methods are actually beneficial to users. As long as I have a Google account, I can log on to a website that uses Google to log on to the service. For more information about this function, see

Flowchart of this function:

 

Description:

Take www.sopif.com as an Example

Http://www.sopif.com/logintest.aspxthis page, there is a button, click the corresponding eventCode

Code
StringS= "Https://www.google.com/accounts/AuthSubRequest? Next = http % 3A % 2f % 2fwww.sopif.com % 2flhking. aspx & scope = http % 3A % 2f % 2fwww.google.com % 2 fcalendar % 2 ffeeds % 2f & session = 1 & secure = 1";
Response. Redirect (s );

The next parameter indicates the redirected page after a successful Google login. scope is the Google service you want to access.

 

On the my next parameter page, lhking. aspx

Page code:

Code
  Object Token;
Protected   Void Page_load ( Object Sender, eventargs E)
{
Token = Request. querystring [ " Token " ];
If (Token ! =   Null )
{
Response. Write ( " You have successfully logged on with your Google account! " + " <Br/> " );
Response. Write (token. tostring ());
}
Else
{
Response. Write ( " You failed to log on with your Google account! " );
}
}

 

That's all. You can test that in this case, Google returns only one token, and the user name and password are safe.

To obtain the email address and name returned by Google:

Please refer to the class. When used, directly response. Redirect (New googlelogin (). getloginurl (); // log on to your Google account

Code
  ///   <Summary>
/// Provide logon to a Google account
///   </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>
/// Generate logon URL
///   </Summary>
///   <Returns> </returns>
Public   Override   String Getloginurl ()
{
Return Urlauthsubrequest +   " Next = "   + Httputility. urlencode (next) +   " & Scope = "   + Httputility. urlencode (scope );
}

///   <Summary>
/// Get the email address and name of the Google account
///   </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 Address
}
Node = Doc. selectsinglenode ( " */* [Local-Name () = 'author']/* [local-Name () = 'name'] " );
If (Node ! =   Null )
{
Name = Node. innertext; // Name
}
RET [ 0 ] = Account;
RET [ 1 ] = Name;
Return RET;
}
Else   Return RET;
}
Catch (Exception ex)
{
New Myexception ( " Googlelogin " , Ex. tostring ());
Return   New   String [] { "" , "" };
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.