A secure implementation of OAuth2 using vanilla ASP.NET C#. Requires no third-party libraries or assemblies.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

67 lines
2.2 KiB

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="GoogleLogin.login" %>
<%
//if the user logs out, this clears the session and reloads the page.
if (Request.QueryString["logout"] == "yes")
{
Session.Clear();
Response.Redirect("/login.aspx");
}
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Google OAuth2 Authentication Demo, by A Better Geek</title>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript" src="scripts.js"></script>
</head>
<body>
<!-- #include file="header.aspx" -->
<!-- BEGIN SAMPLE CODE -->
<%
string code = Request.QueryString["code"];
if (code != null && Session["loggedin"] != "yes")
{
//if there is a "code" value in the querystring and no login session present
//then log in using Google
string client_id = Request.Cookies["client_id"].Value;
string client_secret = Request.Cookies["client_secret"].Value;
string redirect_uri = Request.Cookies["redirect_uri"].Value;
string grant_type = "authorization_code";
//the state token in the return URL needs to be verified first.
string gState = Request["state"];
if (gState == Convert.ToString(Session["state"]))
{
string gurl = "code=" + code + "&client_id=" + client_id +
"&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "&grant_type=" + grant_type;
Response.Write(GoogleLogin(gurl));
}
else
{
Response.Write("<div class='bad'>Please start over. If you're seeing this message, the session ID verification failed.</div>");
}
}
else if (Session["loggedin"] == "yes")
{
Response.Write("<div class='good'>Already logged in. You can <a href='?logout=yes'>logout</a> if you'd like.</div>");
}
else
{
%>
<h2>
<a href="https://accounts.google.com/o/oauth2/auth?client_id=<%=Request.Cookies["client_id"].Value%>&
response_type=code&scope=openid%20email&state=<%=Session["state"]%>&
redirect_uri=<%=Request.Cookies["redirect_uri"].Value%>">Login With Google</a>
</h2>
<%
Response.Write("<div class='alert'>Not logged in.</div>");
}
%>
<!-- END SAMPLE CODE -->
<!-- #include file="footer.aspx" -->
</body>
</html>