如何设置 Azure 身份验证自定义登录 return url?

How to set Azure Authentication custom login return url?

我按照这篇文章 https://azure.microsoft.com/en-us/blog/announcing-app-service-authentication-authorization/ 为我的 MVC 应用程序设置了 Azure 身份验证。首先,我打开了 Azure AD 提供程序。在身份验证/授权设置中,我为 "Action to take when request is not authenticated" 选择了 "Allow request(no Action)" 因为我只需要用户登录某些控制器操作。

然后我添加了一个自定义 FilterAttribute 来检查某个操作是否需要身份验证,如 中所示。在 OnAuthenticationChallenge 函数中,我将这段代码重定向到登录页面:

public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
{
    if (filterContext.Result is HttpUnauthorizedResult) {
        filterContext.Result = new RedirectResult("~/.auth/login/aad");
    }
}

所有这一切都有效,除了在用户完成身份验证后,它被重定向回 mysite/。auth/login/done 页面显示 "You have successfully signed in" 和一个按钮 return 到我网站的基础 url.

我想要的是重定向返回到用户的原始 url,所以我想我需要以某种方式为登录重定向设置 return url。但是我找不到任何关于此的文档。谁能给点建议?

您可以使用 post_login_redirect_url 查询字符串参数来执行此操作。

比如你想在用户登录后自动导航到/welcome.html,你可以设置你的登录重定向到~ /.auth/login/aad?post_login_redirect_url=/welcome.html,用户将被重定向到此页面而不是通用欢迎页面。

谢谢。 这真的很有帮助。 以下对我有用:

     return RedirectToAction(string.Format("login/{0}?post_login_redirect_url=/Home/LoginCallBack", provider), ".auth");

提供商可以是以下字符串之一:google、twitter、microsoftaccount、aad、facebook。

还必须在 Azure 门户中为您的项目配置每个提供程序。

重定向 url 可以是您项目中的任何 uri