0

I'm trying to add Microsoft Azure Active Directory Authentication to an existent ASP.NET Web Application. I can't convert this project to the MVC pattern. The existing application already have an authentication system. I must keep it.

Here is the code used to call the azure authentication service :

HttpContext.Current.GetOwinContext().Authentication.Challenge(
            new AuthenticationProperties { RedirectUri = "/" },
            OpenIdConnectAuthenticationDefaults.AuthenticationType);

The previous code prompt the correct Azure Authentication page. But how can I check if the user authenticated successfully ? In MVC pattern you just have to check Request.IsAuthenticated how can I do the same here ?

Fei Xue
  • 14,369
  • 1
  • 19
  • 27
Lucas S.
  • 312
  • 3
  • 15

1 Answers1

0

Request.IsAuthenticated also works in asp.net web forms application . Please refer to below code :

private void Page_Load(object sender, EventArgs e)
{
    // Check whether the current request has been
    // authenticated. If it has not, redirect the 
    // user to the Login.aspx page.
    if (!Request.IsAuthenticated)
    {
        Response.Redirect("Login.aspx");
    }
}

You could also try :

  bool IsAuthenticated = (System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
Nan Yu
  • 26,101
  • 9
  • 68
  • 148
  • 1
    IsAuthenticated is always false because of my Aure Authentication – Lucas S. Apr 07 '17 at 07:50
  • Request.IsAuthenticated is always false ? – Nan Yu Apr 07 '17 at 08:06
  • When I connect with azure yes, if I connect with the regular form of course it become true. But I want to be able to connect with both methods. – Lucas S. Apr 07 '17 at 08:40
  • I create a new web forms app and enable azure ad login using openid connect , Request.IsAuthenticated works well , testing project [here](https://github.com/yunan2016/WebApplication5) is for your reference (config azure ad setting in web.config) – Nan Yu Apr 07 '17 at 10:15
  • Thanks, I can't manage to make the project work because the code target 4.5.2 can you make it target 4.5 ? Thanks again. – Lucas S. Apr 07 '17 at 13:54
  • You could create your own test application following [this tutorial](http://www.cloudidentity.com/blog/2014/07/24/protecting-an-asp-net-webforms-app-with-openid-connect-and-azure-ad/) – Nan Yu Apr 11 '17 at 02:45