I have made a login page and a user can login. No I want the user to logoff.
I have this in my AccountController:
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
But how can I call this actionresult in my index page?
There are a couple of different ways you can do this
From a view
#Html.ActionLink("Log Out", "LogOff", "Account")
or
Log Out
or
Log Out
I don't recommend the last one however because it doesn't take into consideration action and controller routing. I only included it here as a last resort option.
From another action
You could also call the action directly from code like you would any normal class method, should you need to do that. Like so:
class ActionController : Controller
{
// ...
public ActionResult AnotherAction()
{
// Do stuff here
return LogOff(); // You don't have to return the results if they're not needed
}
// ...
}
like this you can call this actionresult
Logout
Keep a breakpoint in your LogOff action in your Account controller and see what is happening..
its a best practice to make logoff method a Post method , so i suggest to use this form :
<form method='post'>
<button type='submit'>LogOff</button>
</form>
[HttpPost]
public ActionResult Logoff()
{
//Do LogOff Stuff.
}
Related
I want to check for CSRF in my webapp using the [ValidateAntiForgeryToken] attribute. The thing is, I have a GET method that changes data, so it should actually be a POST. But aside from a submit to this action method in the corresponding form, there are multiple redirects to this method, so I can't change it to a POST method. As far as I know, the [ValidateAntiForgeryToken] doesn't work for GET methods. Is there another way to validate a GET method or to redirect within the code to a POST method without using a form?
My action method looks something like this:
public ActionResult SomeAction(SomeModel model)
{
// changes are made in database!!
return View("View", model);
}
And this action method is being redirected to from another action method:
public ActionResult SomeOtherAction()
{
return RedirectToAction("SomeAction", "Controller");
}
I would like to change the first action to:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SomeAction(SomeModel model)
{
// changes are made in database!!
return View("View", model);
}
and add #Html.AntiForgeryToken in the corresponding view. But then the redirect in the second action won't work anymore. Does anyone know a way out of this?
You could return the someAction method...
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult SomeOtherAction()
{
return SomeAction();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SomeAction()
{
return View("SomeAction");
}
}
Index.cshtml
#using (Html.BeginForm("SomeOtherAction", "Home", FormMethod.Get))
{
#Html.AntiForgeryToken();
<button type="submit">Click Me</button>
}
From one controller I want to take user to another page in the app so I say something like:
return RedirectToAction("Index","ThatPage");
And I checked and both ThatPageController and Index do exist.
But it takes the browser to the URL: http://thatpage/
What is going on?
The overload for RedirectToAction where you want to go to another controller and a method within that controller has two separate strings as parameters.
You would want to write:
return RedirectToAction("Index", "Home");
You're missing a " mark in the method signature.
This example when user closes a session
public async Task<ActionResult> Logout()
{
return RedirectToAction("Index", "App");
}
where APP is the controller and Index is the action where I have within the App controller
Appcontroller:
in App controller I have this
public IActionResult Index()
{
return View();
}
When I load the default page (http://localhost/MVCP/Home/index) it loads correctly, whereas when I load another view (http://localhost/MVCP/Home/Create) it doesn't load. How can I fix this?
My Create action in HomeController:
[HttpGet] [ActionName("Create")] public void Create() { }
Q: Do you have an action in your HomeController called Create?
A: Yes, [HttpGet] [ActionName("Create")] public void Create() { }
Your action return value is void and probably you even didn't write anything in response. change the signature of action to have an ActionResult as return a View.
public ActionResult Create()
{
return View();
}
To learn more:
Adding a View
in Getting Started with ASP.NET MVC 5 Series.
May be there is no view you have created so far and it seems your controller are inside area folder so have u checked your routeing too.
I think your Action should return ActionResult or ViewResult but certainly not "void" as you have written currently.
and also you should write
return view();
in Create action
I am trying to make a login form which is located in Layout itself therefor not in any View. Problem is I dont know how to write an ActionResult for the Layout.
If I put my code in lets say ActionResult Index() then it will only work on Index page. So is there something like ActionResult for Layout itself ?
I would do it like this:
Create a login action to return the specific partial.
public ActionResult Login()
{
if (User.Identity.IsAuthenticated)
{
return PartialView("_loggedInPartial");
}
else
{
return PartialView("_notLoggedInPartial");
}
}
Call it within your layout.cshtml like this:
#Url.Action("Login", "Account");
Update
You could also retrieve the user and return it to the _notLoggedInPartial view to display some user credentials or a welcome message like this:
...
else
{
// User retrieval code from db
return PartialView("_notLoggedInPartial", model);
}
...
People generally won't prefer doing this, but still below is the simplest way to achieve
Create a form(html/ajax) in the layout itself with login fields.
Post it from there to your "login post" action.
using (Html.BeginForm("Login", "Account")){
#Html.TextBox("Username")
#Html.TextBox("Password")
<input type="submit" value="Login">
}
AccoutController must contain an action with below structure.
[HttpPost]
Public ActionResult Login(string Username, string Password)
{
//handle appliaction logic
}
What am doing in my application for authorization is that when a user log in one cookie will be created with the user's id there after in every action i l check the cookie and if its not null then proceeds otherwise redirects to log in page assuming the user is been logged out.
Is there any Good method in MVC so that I can avoid the check in every action. Or a good way for authorization.
You may use [Authorize] attribute:
It can't be use for every ActionResult:
[Authorize]
public ActionResult Index()
{
return View()
}
Or for controller. In that case you don't need apply attribute for every action:
[Authorize]
public class HomeController : Controller
{
public ActionResult Index()
{
return View()
}
}
If any action in controller with [Authorize] attribute allows nnonymous use, you may use [AllowAnonymous] attribute for it:
[Authorize]
public class HomeController : Controller
{
public ActionResult Index()
{
return View()
}
[AllowAnonymous]
public ActionResult Edit()
{
return View()
}
}