如何设置Azure身份验证自定义登录返回URL? [英] How to set Azure Authentication custom login return url?

查看:323
本文介绍了如何设置Azure身份验证自定义登录返回URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了这篇文章 https://azure.microsoft.com/zh-cn/blog/announcing-app-service-authentication-authorization/为我的MVC应用设置Azure身份验证.首先,我打开了Azure AD提供程序.在身份验证/授权"设置中,我为未经身份验证的请求时采取的措施"选择了允许请求(不采取任何措施)",因为我只需要用户登录才能执行某些控制器操作.

I followed this article https://azure.microsoft.com/en-us/blog/announcing-app-service-authentication-authorization/ to set up Azure authentication for my MVC app. First I turned on Azure AD provider. In the Authentication / Authorization settings, I selected "Allow request(no Action)" for "Action to take when request is not authenticated" because I only need users to login for certain controller actions.

然后,我添加了一个自定义FilterAttribute来检查是否有一项操作需要进行身份验证,如 https://stackoverflow.com/a/26652816/1837339 .在OnAuthenticationChallenge函数中,我将以下代码重定向到登录页面:

Then I added a custom FilterAttribute to check if one action needs authentication as in https://stackoverflow.com/a/26652816/1837339. In the OnAuthenticationChallenge function, I had this code to redirect to login page:

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

除了用户完成身份验证之后,所有这些工作都可以正常进行,它会重定向回mysite/.auth/login/done页面,显示您已成功登录"和一个返回到我网站的基本URL的按钮.

All of this works, except after user finished authentication, it is redirected back to mysite/.auth/login/done page saying "You have successfully signed in" and a button to return to my site's base url.

我想要的是重定向回到用户的原始URL,所以我认为我需要以某种方式设置登录重定向的返回URL.但是我找不到关于此的任何文档.有人可以提供任何建议吗?

What I want is the redirection goes back to the user's original url, so I think I need somehow set the return url for the login redirect. But I couldn't find any documentation about this. Anyone could give any advice?

推荐答案

您可以使用 post_login_redirect_url 查询字符串参数来做到这一点.

You can use the post_login_redirect_url query string parameter to do this.

例如,如果要在登录后自动将用户导航到/welcome.html ,则可以将登录重定向设置为〜/.auth/login/aad?post_login_redirect_url =/welcome.html ,用户将被重定向到该页面,而不是通用的欢迎页面.

For example, if you want to automatically navigate the user to /welcome.html after logging in, you can set your login redirect to ~/.auth/login/aad?post_login_redirect_url=/welcome.html, and the user will be redirected to this page instead of the generic welcome page.

这篇关于如何设置Azure身份验证自定义登录返回URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆