I am trying to implement the Azure AD authentication on my ASP.NET MVC application (framework version 4.7.2).
All the required config values lie on the web.config
file.
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 }
);
}
}
When I try to invoke the SignIn method it always shows an error, like the below image.
2
Answers
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 astring
and print something inside theif
statement and something else out; something like this:From there, you can see if it’s not finding the
SignIn
endpoint or the redirection path on/Default
that redirects to/Home/Index/
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:Response:
To resolve the error, make sure to add
{0}
at the end of your AADInstance key value by modifying Web.config file:Now, I ran the project again by making above change and got below screen with
Sign In
button:When I clicked
Sign in
button, I got below screen to pick Azure AD account:Once sign in successful, it took me to
/Home/Index
page:Reference:
asp.net mvc – Azure AD Signin from MVC: Response status code does not indicate success: 404 (Not Found) by AxleWack