0

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.

2
  • You can try [Authorize] attribute for this. Commented Oct 20, 2013 at 5:29
  • Do you use FormsAuthentication? Commented Oct 20, 2013 at 21:12

1 Answer 1

1

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()
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

where should i define autherize.. I mean I need to check the cookie value na?

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.