Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Azure Authentication custom login return url?

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.

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");
    }
}

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.

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?

like image 344
bigbearzhu Avatar asked Dec 18 '15 03:12

bigbearzhu


1 Answers

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

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.

like image 75
Chris Gillum Avatar answered Oct 12 '22 12:10

Chris Gillum