i am using mvc 3 code first. facing problem while passing data form SecurityAttribute class to Controller. i actually want to redirect user on login page with displaying Message. for this i override AuthorizeCore method in SecurityAttribute class. in this method i am unable to direct use session, cookies, tempdate, and viewbag etc. any other solution to solve this problem. Thanks
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext.Session["UserID"] == null)
{
//here i am unable to pass message to User/LogOn action.
httpContext.Response.Redirect("~/User/LogOn");
// httpContext.Session["lblMsg"] = "You are not authroize to perform this action.Please Login through different account";
return false;
}
First things first, you should not redirect inside the
AuthorizeCoremethod. You should use theHandleUnauthorizedRequestmethod which is intended for this purpose. As far as passing an error message to the LogOn action is concerned you could use TempData:and then inside the LogOn action:
or if you want to access it inside the
LogOn.cshtmlview:Another possibility is to pass the message as a query string parameter instead of using TempData:
and then you could have the
LogOnaction take the error message as action parameter: