This repository has been archived by the owner on Jun 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from flosim/SimpleADALSample
Simple adal sample
- Loading branch information
Showing
9 changed files
with
180 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Simple ADAL.JS Sample | ||
===================== | ||
|
||
This simple example is intended to show how to use the adal.js methods to authenticate with Office 365 and obtain a token that can be used in REST calls to the Office 365 API. | ||
The sample uses a simple HTML page and about 50 lines of JavaScript and only depends on adal.js and jQuery. | ||
|
||
## Running the sample | ||
|
||
Modify the configuration settings in app.js and ensure that you have your app settings in Azure AD . | ||
|
||
Open a new website in Visual Studio (File->New->Web site...) selecting the directory containing these files. Visual Studio will allocate a | ||
port number which is visible if you inspect the project properties. This address and port will need to be included in the list of redirect URLs | ||
in your Azure AD app settings. | ||
|
||
Clicking on the login button should take you to the login page. After you have logged in you should see your user id and name in the page. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --> | ||
|
||
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> | ||
<!-- | ||
In the example below, the "SetAttributes" transform will change the value of | ||
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator | ||
finds an attribute "name" that has a value of "MyDB". | ||
<connectionStrings> | ||
<add name="MyDB" | ||
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" | ||
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> | ||
</connectionStrings> | ||
--> | ||
<system.web> | ||
<compilation xdt:Transform="RemoveAttributes(debug)" /> | ||
<!-- | ||
In the example below, the "Replace" transform will replace the entire | ||
<customErrors> section of your web.config file. | ||
Note that because there is only one customErrors section under the | ||
<system.web> node, there is no need to use the "xdt:Locator" attribute. | ||
<customErrors defaultRedirect="GenericError.htm" | ||
mode="RemoteOnly" xdt:Transform="Replace"> | ||
<error statusCode="500" redirect="InternalError.htm"/> | ||
</customErrors> | ||
--> | ||
</system.web> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0"?> | ||
|
||
<!-- | ||
For more information on how to configure your ASP.NET application, please visit | ||
http://go.microsoft.com/fwlink/?LinkId=169433 | ||
--> | ||
|
||
<configuration> | ||
|
||
<system.web> | ||
<compilation debug="true" targetFramework="4.5" /> | ||
<httpRuntime targetFramework="4.5" /> | ||
</system.web> | ||
|
||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
(function () { | ||
// Simple JavaScript to demonstrate how to use ADAL.JS | ||
var config = | ||
{ | ||
tenant: 'contoso.onmicrosoft.com', // replace with your tenant id (e.g. contoso.onmicrosoft.com) | ||
clientId: '79f10513-ca82-4e6a-8cc9-f10513ff6928', // replace with your clientID (GUID) | ||
extraQueryParameter: 'nux=1', | ||
url: window.location.href // use current page URL | ||
}; | ||
|
||
var authContext = new AuthenticationContext(config); | ||
|
||
if (authContext.isCallback(window.location.hash)) { | ||
// callback from Azure AD auth page | ||
var requestInfo = authContext.getRequestInfo(window.location.hash); | ||
authContext.saveTokenFromHash(requestInfo); | ||
window.location.hash = ''; | ||
} | ||
|
||
// bind to button events | ||
jQuery("#login").click(function () { | ||
authContext.login(); | ||
}); | ||
jQuery("#logout").click(function () { | ||
authContext.logOut(); | ||
}); | ||
|
||
var resource = authContext.getResourceForEndpoint(config.url); | ||
var token = authContext.getCachedToken(resource); // auth token to be used against Office365 API | ||
var isAuthenticated = token !== null && token.length > 0; | ||
var loginError = authContext.getLoginError() || ''; | ||
|
||
var user = authContext.getCachedUser(); | ||
if (isAuthenticated && user !== null) { | ||
jQuery("#userid").text(user.userName); | ||
var name = ""; | ||
if (user.profile != null) name = user.profile.given_name + " " + user.profile.family_name; | ||
jQuery("#username").text(name); | ||
// TODO: demonstrate Office365 API REST call using bearer token | ||
// (requires iframe to work in browser because of cross-site request) | ||
} | ||
else { | ||
jQuery("#userid").text('user is not logged in'); | ||
jQuery("#username").text(''); | ||
} | ||
jQuery("#error").text(loginError); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Simple ADAL.JS Demo</title> | ||
</head> | ||
<body> | ||
<h1>Simple ADAL.JS Demo</h1> | ||
<p>This sample demonstrates how to take use ADAL.JS for adding Azure AD authentication to a simple HTML/JavaScript app.</p> | ||
<h2 id="userid"></h2> | ||
<h2 id="username"></h2> | ||
<h2 id="error"></h2> | ||
<br /> | ||
<button id="login">Login</button> | ||
<button id="logout">Logout</button> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | ||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> | ||
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/0.0.4/js/adal.min.js"></script> | ||
<script src="app.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters