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.
 
 
 
 

94 lines
3.7 KiB

<h2>Google OAuth2 Demonstration v1.0</h2>
<p>
This is an example application implementing Google OAuth2 authentication for a C# web application.
This software is provided AS IS by A Better Geek and is solely for testing purposes. Please remember
to test all code thoroughly in your own application before moving to production.
</p>
<p>
Some notes:
</p>
<ul>
<li>When viewing the source code of this ASPX page, please focus on the code between the "SAMPLE CODE" commented lines.</li>
<li>Click the "Delete Cookies" button when you're done testing, or to reset your saved client ID and client secret.</li>
<li>The code behind page is heavily commented to aid in your own understanding of the authentication process.</li>
<li>For your reference, this sample project is based on the following A Better Geek articles:
<ul>
<li>
<a href="http://blog.abettergeek.com/web-development/getting-started-google-authentication-using-oauth2-and-asp-net-c/"
target="_blank">
Part one: Getting Started
</a>
</li>
<li>
<a href="http://blog.abettergeek.com/web-development/logging-in-google-authentication-using-oauth2-and-asp-net-c/"
target="_blank">
Part two: Logging In
</a>
</li>
<li>
<a href="http://blog.abettergeek.com/web-development/validating-integrity-google-authentication-using-oauth2-and-asp-net-c/"
target="_blank">
Part three: Validating Integrity
</a>
</li>
</ul>
</li>
</ul>
<hr />
<% if (Request.Cookies["client_id"] == null && Request.Form["client_id"] == null && Session["loggedin"] != "yes") { %>
<p class="alert">
You are seeing this message because you have not configured the application yet. Setting the below
parameters will create local browser cookies storing each parameter. In order to protect your application's
client secret, please remember to <b>clear the cookies for this page when you are done testing</b>!
</p>
<p>
If you haven't already done so, you need to first go to <a href="http://code.google.com/apis/console" target="_blank">Google's API Console</a>
and create your application. Note down your <b>client id</b> and <b>client secret</b> for use in the below form.
You also need to use the URL of this page, including the port number for localhost, as the <b>redirect URI</b> for
your application.
</p>
<form id="setCookies" name="setCookies" method="post" action="login.aspx" onsubmit="return validateForm();">
<div class="row">
<div class="label">
<label for="client_id">Client ID:</label>
</div>
<div class="input">
<input type="text" name="client_id" id="client_id" />
</div>
</div>
<div class="row">
<div class="label">
<label for="client_secret">Client secret:</label>
</div>
<div class="input">
<input type="text" name="client_secret" id="client_secret" />
</div>
</div>
<div class="row">
<div class="label">
<label for="redirect_uri">Redirect URI:</label>
</div>
<div class="input">
<input type="text" name="redirect_uri" id="redirect_uri" readonly="readonly" value="<%=Request.Url.AbsoluteUri%>" />
</div>
</div>
<div class="row">
<div class="label"></div>
<div class="submit">
<input type="submit" id="submitForm" value="Save Settings" />
</div>
</div>
</form>
<% } else if (Request.Cookies["client_id"] == null && Request.Form["client_id"] != null) { %>
<%
//write POSTDATA to cookies
Response.Cookies["client_id"].Value = Request.Form["client_id"];
Response.Cookies["client_secret"].Value = Request.Form["client_secret"];
Response.Cookies["redirect_uri"].Value = Request.Form["redirect_uri"];
Response.Redirect(Request.Form["redirect_uri"]);
%>
<% } else { %>