Related Tags:
C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, MVC 4 It is a newer version of ASP.NET MVC,ASP.NET MVC 4 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of ASP.NET and the .NET Framework. Learn More,

how to implement return url functionality for login page in asp.net mvc 4?

Description:I have a simple application in asp.net mvc 4 with my controller method for login is with attribute [AllowAnonymous] and I want my users to redirect to same page from which they have been redirected to the login page. I have seen some article which suggests that I need to pass the returnurl in the query string but did't get the complete process of implementing the feature.
kindly suggests a complete solution!

Posted by: | Posted on: Jul 30, 2018

2 answers

Replies

4

Simple solution could be as follows
Your controller get method will be like the following method taking the returnUrl as input parameter and setting it to the viewbag.

[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}

Now in your MVC view you will be setting the ViewBag.ReturnUrl to ReturnUrl parameter of beginForm as shown below.

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))

Now in your controller post method you will take the your LoginModel as well as returnUrl as an extra parameter.

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
// your login login logic would be added here as well.
if (ModelState.IsValid))
{
return RedirectToLocal(returnUrl);
}
// In case of failure form displayed again
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}


Following method is called in above controller method to make sure that the returnUrl is local url.

private ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}

Replied by: | Replied on: Aug 28, 2018



0

<a href=https://megaremont.pro/brest-restavratsiya-vann>materials for the restoration of baths</a>

Replied by: | Replied on: Jul 11, 2023



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview