forked from DotNetOpenAuth/DotNetOpenAuth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enabled AzureAD integration and added TestAzureAD sample app
- Loading branch information
Showing
159 changed files
with
33,861 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -15,7 +15,6 @@ PrecompiledWeb | |
*.user | ||
*.tmp | ||
*.ldf | ||
*.mdf | ||
*.swx | ||
.dotest | ||
*.Publish.xml | ||
|
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,77 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="AzureADClaims.cs" company="Microsoft"> | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace DotNetOpenAuth.ApplicationBlock | ||
{ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.Serialization; | ||
|
||
/// <summary> | ||
/// Contains clains of a AzureAD token. | ||
/// </summary> | ||
/// <remarks> | ||
/// Technically, this class doesn't need to be public, but because we want to make it serializable in medium trust, it has to be public. | ||
/// </remarks> | ||
[DataContract] | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "AzureAD", Justification = "Brand name")] | ||
public class AzureADClaims | ||
{ | ||
#region Public Properties | ||
|
||
/// <summary> | ||
/// Gets or sets the audience. | ||
/// </summary> | ||
/// <value> The audience token is valid for. </value> | ||
[DataMember(Name = "aud")] | ||
public string Aud { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the issuer. | ||
/// </summary> | ||
/// <value> The issuer. </value> | ||
[DataMember(Name = "iss")] | ||
public string Iss { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the early expiry time. | ||
/// </summary> | ||
/// <value> The early expiry time. </value> | ||
[DataMember(Name = "nbf")] | ||
public string Nbf { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the expiry time. | ||
/// </summary> | ||
/// <value> The expiry time. </value> | ||
[DataMember(Name = "exp")] | ||
public string Exp { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the id of the user. | ||
/// </summary> | ||
/// <value> The id of the user. </value> | ||
[DataMember(Name = "oid")] | ||
public string Oid { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the id of the tenant. | ||
/// </summary> | ||
/// <value> The tenant . </value> | ||
[DataMember(Name = "tid")] | ||
public string Tid { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the appid of application. | ||
/// </summary> | ||
/// <value> The id of the application. </value> | ||
[DataMember(Name = "appid")] | ||
public string Appid { get; set; } | ||
#endregion | ||
} | ||
} |
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,62 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="AzureADGraph.cs" company="Microsoft"> | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace DotNetOpenAuth.ApplicationBlock | ||
{ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.Serialization; | ||
|
||
/// <summary> | ||
/// Contains data of a AzureAD user. | ||
/// </summary> | ||
/// <remarks> | ||
/// Technically, this class doesn't need to be public, but because we want to make it serializable in medium trust, it has to be public. | ||
/// </remarks> | ||
[DataContract] | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "AzureAD", Justification = "Brand name")] | ||
public class AzureADGraph { | ||
#region Public Properties | ||
|
||
/// <summary> | ||
/// Gets or sets the firstname. | ||
/// </summary> | ||
/// <value> The first name. </value> | ||
[DataMember(Name = "givenName")] | ||
public string GivenName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the lastname. | ||
/// </summary> | ||
/// <value> The last name. </value> | ||
[DataMember(Name = "surname")] | ||
public string Surname { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the email. | ||
/// </summary> | ||
/// <value> The email. </value> | ||
[DataMember(Name = "userPrincipalName")] | ||
public string UserPrincipalName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the fullname. | ||
/// </summary> | ||
/// <value> The fullname. </value> | ||
[DataMember(Name = "displayName")] | ||
public string DisplayName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the id. | ||
/// </summary> | ||
/// <value> The id. </value> | ||
[DataMember(Name = "objectId")] | ||
public string ObjectId { get; set; } | ||
#endregion | ||
} | ||
} |
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,51 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="AzureADHeader.cs" company="Microsoft"> | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace DotNetOpenAuth.ApplicationBlock | ||
{ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.Serialization; | ||
|
||
/// <summary> | ||
/// Contains header of AzureAD JWT token. | ||
/// </summary> | ||
/// <remarks> | ||
/// Technically, this class doesn't need to be public, but because we want to make it serializable in medium trust, it has to be public. | ||
/// </remarks> | ||
[DataContract] | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "AzureAD", Justification = "Brand name")] | ||
|
||
public class AzureADHeader | ||
{ | ||
#region Public Properties | ||
|
||
/// <summary> | ||
/// Gets or sets the type of token. Will always be JWT | ||
/// </summary> | ||
/// <value> The type of token. </value> | ||
[DataMember(Name = "typ")] | ||
public string Typ { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the algo of the header. | ||
/// </summary> | ||
/// <value> The algo of encoding. </value> | ||
[DataMember(Name = "alg")] | ||
public string Alg { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the thumbprint of the header. | ||
/// </summary> | ||
/// <value> The thumbprint of the cert used to encode. </value> | ||
[DataMember(Name = "x5t")] | ||
public string X5t { get; set; } | ||
|
||
#endregion | ||
} | ||
} |
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
112 changes: 112 additions & 0 deletions
112
samples/DotNetOpenAuth.ApplicationBlock/azureadclient.cs
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,112 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="AzureADClient.cs" company="Microsoft"> | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace DotNetOpenAuth.ApplicationBlock | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Web.Script.Serialization; | ||
using DotNetOpenAuth.OAuth2; | ||
|
||
public class AzureADClient : WebServerClient | ||
{ | ||
private static readonly AuthorizationServerDescription AzureADDescription = new AuthorizationServerDescription | ||
{ | ||
TokenEndpoint = new Uri("https://login.windows.net/global/oauth2/token"), | ||
AuthorizationEndpoint = new Uri("https://login.windows.net/global/oauth2/authorize?resource=00000002-0000-0000-c000-000000000000/graph.windows.net"), | ||
}; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AzureADClient"/> class. | ||
/// </summary> | ||
public AzureADClient() | ||
: base(AzureADDescription) | ||
{ | ||
} | ||
|
||
#region Methods | ||
|
||
/// <summary> | ||
/// Parses the access token into an AzureAD token. | ||
/// </summary> | ||
/// <param name="token"> | ||
/// The token as a string. | ||
/// </param> | ||
/// <returns> | ||
/// The claims as an object and null in case of failure. | ||
/// </returns> | ||
public AzureADClaims ParseAccessToken(string token) | ||
{ | ||
try | ||
{ | ||
// This is the encoded JWT token split into the 3 parts | ||
string[] strparts = token.Split('.'); | ||
|
||
// Decparts has the header and claims section decoded from JWT | ||
string jwtHeader, jwtClaims; | ||
string jwtb64Header, jwtb64Claims, jwtb64Sig; | ||
byte[] jwtSig; | ||
if (strparts.Length != 3) | ||
{ | ||
return null; | ||
} | ||
jwtb64Header = strparts[0]; | ||
jwtb64Claims = strparts[1]; | ||
jwtb64Sig = strparts[2]; | ||
jwtHeader = Base64URLdecode(jwtb64Header); | ||
jwtClaims = Base64URLdecode(jwtb64Claims); | ||
jwtSig = Base64URLdecodebyte(jwtb64Sig); | ||
|
||
JavaScriptSerializer s1 = new JavaScriptSerializer(); | ||
|
||
AzureADClaims claimsAD = s1.Deserialize<AzureADClaims>(jwtClaims); | ||
AzureADHeader headerAD = s1.Deserialize<AzureADHeader>(jwtHeader); | ||
|
||
return claimsAD; | ||
} | ||
catch (Exception) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Base64 decode function except that it switches -_ to +/ before base64 decode | ||
/// </summary> | ||
/// <param name="str"> | ||
/// The string to be base64urldecoded. | ||
/// </param> | ||
/// <returns> | ||
/// Decoded string as string using UTF8 encoding. | ||
/// </returns> | ||
private static string Base64URLdecode(string str) | ||
{ | ||
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); | ||
return encoder.GetString(Base64URLdecodebyte(str)); | ||
} | ||
|
||
/// <summary> | ||
/// Base64 decode function except that it switches -_ to +/ before base64 decode | ||
/// </summary> | ||
/// <param name="str"> | ||
/// The string to be base64urldecoded. | ||
/// </param> | ||
/// <returns> | ||
/// Decoded string as bytes. | ||
/// </returns> | ||
private static byte[] Base64URLdecodebyte(string str) | ||
{ | ||
// First replace chars and then pad per spec | ||
str = str.Replace('-', '+').Replace('_', '/'); | ||
str = str.PadRight(str.Length + ((4 - (str.Length % 4)) % 4), '='); | ||
return Convert.FromBase64String(str); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
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,17 @@ | ||
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AzureAD.aspx.cs" Inherits="OAuthClient.AzureAD" Async="true"%> | ||
|
||
<!DOCTYPE html> | ||
|
||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head runat="server"> | ||
<title></title> | ||
</head> | ||
<body> | ||
<form id="form1" runat="server"> | ||
<div> | ||
Welcome, | ||
<asp:Label Text="[name]" ID="nameLabel" runat="server" /> | ||
</div> | ||
</form> | ||
</body> | ||
</html> |
Oops, something went wrong.