我正在尝试在我的 ASP.NET MVC 应用程序(框架版本 4.7.2)上实现 Azure AD 身份验证。
所有必需的配置值都位于
web.config
文件中。
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 }
);
}
}
当我尝试调用 SignIn 方法时,它总是显示错误,如下图所示。
我知道这不会是一个“答案”,但我会帮助阐明一些如何发生以及发生了什么(找出它失败的地方)。
为了调试您的行为,我建议将
void
更改为 string
并在 if
语句中打印一些内容以及其他内容;像这样的:
public string SignIn()
{
if (!Request.IsAuthenticated)
{
//HttpContext.GetOwinContext().Authentication.Challenge(
// new AuthenticationProperties { RedirectUri = "/Default" },
// OpenIdConnectAuthenticationDefaults.AuthenticationType
//);
return "Not Authenticated"
}
return "Authenticated"
}
从那里,您可以查看是否找不到
SignIn
端点或 /Default
上重定向到 /Home/Index/
的重定向路径