0

I am struggling over this issue since yesterday.I am working on a web application which includes email service.Email includes 'link' to a certain page of that application.

Now:I have 2 scenarios:

1) If the user is logged in(in the application) already and he hit the url he got in his email,the link will open perfectly.

2) If the user is not logged in(in the application) then the url will not open and will redirect itself to the login page with the functionality added in the BaseController.

*

Now what I want is when the user logs in after hitting the url and on successfully login the user gets redirect to the link provided in the Email.

*

for eg: If the user gets an email to the detail page of an employee,So on successfully login the user gets redirect to the Detail page of the employee.

Something like appending the redirecturl to the login page.

I think rewriting url is what I should be doing.

But I dont know how can I use that in this case.Help me out.

1
  • You just need to persist the returnUrl across request and it should work with small change. See my answer below. Commented Oct 17, 2014 at 8:15

3 Answers 3

0

The default project template that comes with ASP.NET MVC 5 behaves exactly as you describe.

If you want to redirect to a custom login URL, reconfigure the LoginPath property of the CookieAuthenticationOptions object

LoginPath = new PathString("/Account/Login")

In the default template this is done in the Startup.Auth.cs class.

NOTE: If you are using an old version of ASP.NET MVC, the default project template behaved in the same way. But previously this was implemented using Forms Authentication, so in order to redirect to a custom login URL you would then have to set the loginUrl attribute of the <forms> tag in the Web.config file

Sign up to request clarification or add additional context in comments.

4 Comments

M working on asp.net mvc 4.5 application.and it doesnt provide any startup.auth.cs class. What should i do now?
Have a look at the default project template. You can view this by starting a new project in Visual Studio 2013 File -> New Project -> ASP.NET Web Application Then make sure to chose the "MVC" template, not the "Empty" template
I am using vs2012 and it doesnt provide any default template like you are suggesting
VS2012 includes MVC4, which uses the old model I described. In vs2012 you can simply start a new MVC4 project (File -> New Project -> ASP.NET MVC 4 Web Application) and then choose the "Internet application" template. This will use FormsAuthentication, where you can set the loginUrl attribute of the <forms> tag in the Web.config file if you need to redirect to a custom login URL
0
  1. By default if a user tries to access the authorized page when he is not authorized the automatically gets redirected to the log in page or the page which is configured in web.config file for the element. And you can see the query string returnUrl having the url that was tried to access initially get appended to the log in url.
  2. To access the return url, include a new parameter as returnUrl and maintain the return url in a hidden field by model data to access on post back for redirection.
  3. If the user is authenticated then on post back then redirect the user to the specified page what he intended to go for.

2 Comments

Actually the link doesnt get appended in the querystring
@Farzi. Are you using custom authorization attribute, else create it and in the OnActionExecuting method check whether the user is authenticated or not. If authenticated then do nothing if not then get the url referrer and redirect the user to the log in page with providing a query string returnUrl with the referrer url. Also do validate the referrer url that the referrer is from the same domain only. You can use register this filter globally. Then you can follow the steps as I suggested in my answer.
0

I don't remember exactly but few month ago I implemented similar functionality and i had to save returnUrl explicitly (due to MVC bug or something) - Refer this link

AccountController.cs - Snapshot

        [HttpGet]
        [AllowAnonymous]
        public ActionResult Login(string returnUrl, string userName)
        {
            // You login method logic....

            // Add this line to save the returnUrl value
            ViewBag.ReturnUrl = returnUrl;    
        }

Login.cshtml - Snapshot

@using (Html.BeginForm("Login", "Account", FormMethod.Post ,new {ReturnUrl = ViewBag.ReturnUrl}))
{
  <input type="hidden" name="ReturnUrl" value="@Request.QueryString["ReturnUrl"]" />

 // .....              
}

See if this helps in your case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.