Using Microsoft Graph with ASP.Net Application to sign in using your Office365 Tenant

Create and empty ASP.Net MVC Project, in App_start folder create a class name it Startup.cs and paste bellow code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using Owin;
using System.Configuration;
using System.Globalization;
using System.Threading.Tasks;

[assembly: OwinStartup(typeof(WebApplication3.App_Start.Startup))]
namespace WebApplication3.App_Start
{
public class Startup
{
private static string Client_ID = ConfigurationManager.AppSettings[“ida:ClientId”];
private static string AzureADInstance = ConfigurationManager.AppSettings[“ida:AADInstance”];
private static string YourOffice365Tenent = ConfigurationManager.AppSettings[“ida:Tenant”];
private static string LogoutRedirect = ConfigurationManager.AppSettings[“ida:PostLogoutRedirectUri”];

string authority = String.Format(CultureInfo.InvariantCulture, AzureADInstance, YourOffice365Tenent);

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

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

app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = Client_ID,
Authority = authority,
PostLogoutRedirectUri = LogoutRedirect,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = context =>
{
context.HandleResponse();
context.Response.Redirect(“/Error/message=” + context.Exception.Message);
return Task.FromResult(0);
}
}
});
}
}
}

create and empty controller name it AccountController and paste bellow code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;

namespace WebApplication3.Controllers
{
public class AccountController : Controller
{
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
);
}
}
}

in your shared folder create _LoginPartial.cshtml a partial view and past bellow code:

@if (Request.IsAuthenticated)
{
<text>
<ul class=”nav navbar-nav navbar-right”>
<li class=”navbar-text”>
Hello, @User.Identity.Name!
</li>
<li>
@Html.ActionLink(“Sign out”, “SignOut”, “Account”)
</li>
</ul>
</text>
}
else
{
<ul class=”nav navbar-nav navbar-right”>
<li>@Html.ActionLink(“Sign in”, “SignIn”, “Account”, routeValues: null, htmlAttributes: new { id = “loginLink” })</li>
</ul>
}

Modify your web.config file with and past the following code inside appSettings tage

<add key=”ida:ClientId” value=”757b7f21-2064-499b-88bf-24984d0d2227″ />
<add key=”ida:AADInstance” value=”https://login.microsoftonline.com/{0}” />
<add key=”ida:Tenant” value=”jalalmx.onmicrosoft.com” />
<add key=”ida:PostLogoutRedirectUri” value=”https://localhost:44393/&#8221; />

Now go to your Office365 Tenant site admin then go to Azure Active directory admin center

under authentication make sure you check the ID tokens is checked

 

In your Azure AD Admin center go to App registration click New registration give your app name

and past your https Url of your ASP.Net Application see image bellow

Now when you click sing in it will redirect you to Office365 login and you can login with your Office365 Tenant account.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.