简体   繁体   中英

Redirect user to Login page with message

I need to display a message to a user if he or she is not registered or signed up before accessing a page

[Authorize]
public ActionResult LearningTopics()
{

}

As it is now the user is directed to the login page.

How can I determine as to whether the user was directed to the Login page because of the [Authorize] attribute so that I can display a message like 'Please login to access learner topics.'

You could check the Return URL Parameter on the page in either the server code or the view code to determine if the page was redirected or not.

For example, if your return URL parameter was returnUrl then in the log in route you could do

if (!String.IsNullOrEmpty(returnUrl)) { 
    // add a message "Please register" to the view.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM