skip to Main Content

I am trying to implement the Azure AD authentication on my ASP.NET MVC application (framework version 4.7.2).

enter image description here

All the required config values lie on the web.config file.

enter image description here

AccountController.cs

public class AccountController : Controller
{
    // GET: Account
    public ActionResult Index()
    {
        return View();
    }

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

    public void SignOut()
    {

        HttpContext.GetOwinContext().Authentication.SignOut(
          OpenIdConnectAuthenticationDefaults.AuthenticationType,
          CookieAuthenticationDefaults.AuthenticationType
        );
    }
}

RouteConfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

enter image description here

When I try to invoke the SignIn method it always shows an error, like the below image.

enter image description here

2

Answers


  1. I know this won’t be an "answer" as it is, but I would be helpful to shed some light of how and what’s happening (find where is it failing).

    In order to debug your behaviour, I would suggest to change the void for a string and print something inside the if statement and something else out; something like this:

    public string SignIn()
    {
        if (!Request.IsAuthenticated)
        {
            //HttpContext.GetOwinContext().Authentication.Challenge(
            //    new AuthenticationProperties { RedirectUri = "/Default" },
            //    OpenIdConnectAuthenticationDefaults.AuthenticationType
            //);
            return "Not Authenticated"
        }
        return "Authenticated"
    }
    

    From there, you can see if it’s not finding the SignIn endpoint or the redirection path on /Default that redirects to /Home/Index/

    Login or Signup to reply.
  2. The error occurred as you missed adding {0} to the AADInstance key in your Web.config file.

    I followed this blog by @ayoub to implement the Azure AD authentication in my .NET MVC application (framework 4.7.2).

    When I click Sign In button without adding {0} to the AADInstance key in Web.config file, I too got same error:

    <appSettings>
        <add key="ida:ClientId" value="appID" />  
        <add key="ida:Tenant" value="xxxxxxxx.onmicrosoft.com" />  
        <add key="ida:AADInstance" value="https://login.microsoftonline.com/" />  
        <add key="ida:PostLogoutRedirectUri" value="https://localhost:44353/" />
    </appSettings>
    

    Response:

    enter image description here

    To resolve the error, make sure to add {0} at the end of your AADInstance key value by modifying Web.config file:

    <appSettings>
        <add key="ida:ClientId" value="appID" />  
        <add key="ida:Tenant" value="xxxxxxxx.onmicrosoft.com" />  
        <add key="ida:AADInstance" value="https://login.microsoftonline.com/{0}" />  
        <add key="ida:PostLogoutRedirectUri" value="https://localhost:44353/" />
    </appSettings>
    

    Now, I ran the project again by making above change and got below screen with Sign In button:

    enter image description here

    When I clicked Sign in button, I got below screen to pick Azure AD account:

    enter image description here

    Once sign in successful, it took me to /Home/Index page:

    enter image description here

    Reference:
    asp.net mvc – Azure AD Signin from MVC: Response status code does not indicate success: 404 (Not Found) by AxleWack

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search