Skip to content

Instantly share code, notes, and snippets.

@keith9820
Last active December 30, 2016 22:02
Show Gist options
  • Save keith9820/22aea807e9cf0cd514f75ed9458d6b33 to your computer and use it in GitHub Desktop.
Save keith9820/22aea807e9cf0cd514f75ed9458d6b33 to your computer and use it in GitHub Desktop.
Register existing app to use Azure AD

To Configure Admin Panel for AD Integrated Security:

  • Set SSL = True
  • Install Nuget Packages:
    • Install-Package Microsoft.IdentityModel.Protocol.Extensions
    • Install-Package Microsoft.Owin.Security.OpenIdConnect
    • Install-Package Microsoft.Owin.Security.Cookies
    • Install-Package Microsoft.Owin.Host.SystemWeb
    • Install-Package System.IdentityModel.Tokens.Jwt
  • In web.config
<appSettings>
  <add key=“ida:ClientId” value=“” />
  <add key=“ida:AAdInstance” value=“https://login.microsoftonline.com/{0}” />
  <add key=“ida:Tenant” value=“sahiltest.onmicrosoft.com” />
  <add key=“ida:PostLogoutRedirectUri” value=“https://localhost:44300” />
</appSettings>
  • In App_Start/Startup.cs
using Owin;
using System.Configuration;
using System.Globalization;
using Microsoft.Owin.Security; 
using Microsoft.Owin.Security.Cookies; 
using Microsoft.Owin.Security.OpenIdConnect; 
using System.Threading.Tasks;

[assembly:OwinStartup(typeof(BrowserToWebApp.App_Start.Startup))]
Namespace BrowserToWebApp.App_Start
{
  public class Startup
  {
	private static string clientId = ConfigurationManager.AppSettings[“ida:ClientId”];
	private static string aaInstance = ConfigurationManager.AppSettings[“ida:AADInstance”];
	private static string tenant = ConfigurationManager.AppSettings[“ida:Tenant”];
	private static string postLogoutRedirectUri = ConfigurationManager.AppSettings[“ida:PostLogoutRedirectUri”];

	string authority = string.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

	public void Configuration(IAppBuilder app)
	{
		ConfigureAuth(app);
	}

	public void ConfigureAuth(IAppBuilder app)
	{
		app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
		app.UseCookieAuthentication(new CookieAuthenticationOptions());

		app.UseOpenIdConnectAuthentication(
		{
			ClientId = clientId;
			Authority = authority;
			PostLogoutRedirectUri = postLogoutRedirectUri;
			Notifications = new OpenIdConnectAuthenticationNotifications
			{
				AuthenticationFailed = context =>
				{
					context.HandledResponse();
					context.Response.Redirect($“/Error/messsage={context.Exception.Message}”);
					return Task.FromResult(0);
				}
			}
		});
	}
  }
}
  • In controller
using Microsoft.Owin.Security; 
using Microsoft.Owin.Security.Cookies; 
using Microsoft.Owin.Security.OpenIdConnect; 

Public void SignIn()
{
	if(!Request.IsAuthenticated)
	{
		HttpContext.GetOwinContext().Authentication.Challenge(
			new AuthenticationProperties{ RedirectUri = “/“}, OpenIdConnectAuthenticationDefaults.AuthenticationType);
	}
}

Public void SignOut()
{
	HttpContext.GetOwinContext().Authentication.SignOut(
			OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
		);

}

  • Register app with Azure AD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment