Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8092047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:12:15+00:00 2026-06-05T20:12:15+00:00

i am using mvc 3 code first. facing problem while passing data form SecurityAttribute

  • 0

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; 
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:12:16+00:00Added an answer on June 5, 2026 at 8:12 pm

    First things first, you should not redirect inside the AuthorizeCore method. You should use the HandleUnauthorizedRequest method which is intended for this purpose. As far as passing an error message to the LogOn action is concerned you could use TempData:

    public class SecurityAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            // perform the custom authorization logic here and return true or false
            // DO NOT redirect here
            return httpContext.Session["UserID"] != null;
        }
    
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.Controller.TempData["ErrorMessage"] = "You are not authroize to perform this action.Please Login through different account";
    
            // calling the base method will actually throw a 401 error that the
            // forms authentication module will intercept and automatically redirect
            // you to the LogOn page that was defined in web.config
            base.HandleUnauthorizedRequest(filterContext);
        }
    }
    

    and then inside the LogOn action:

    public ActionResult LogOn()
    {
        string errorMessage = TempData["ErrorMessage"] as string;
        ...
    }
    

    or if you want to access it inside the LogOn.cshtml view:

    <div>@TempData["ErrorMessage"]</div>
    

    Another possibility is to pass the message as a query string parameter instead of using TempData:

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        var values = new RouteValueDictionary(new
        {
            controller = "User",
            action = "LogOn",
            errormessage = "You are not authroize to perform this action.Please Login through different account"
        });
        filterContext.Result = new RedirectToRouteResult(values);
    }
    

    and then you could have the LogOn action take the error message as action parameter:

    public ActionResult LogOn(string errorMessage)
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Setup Using MVC 3 + Code First Here are my classes public class Member
I'm using ASP.NET MVC 3 code-first and I have added validation data annotations to
I am using MVC and entity framework. I started with code first design and
I'm using EF Code first with my asp.net mvc application. here is my code:
I have an MVC 4 project using EF 5 Code First. I am trying
I have an ASP.NET MVC 3 application using an Entity Framework (4.3.1) Code First
I'm trying to create a many-to-many relationship using EF code first in my MVC
I'm using ASP.NET MVC 3 with the Entity Framework 4 code first approach and
I am currently using ASP.NET MVC and Entity Framework Code First using the repository
I am using code first approach in a ASP.NET MVC 3 application and all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.