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 6627703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:02:43+00:00 2026-05-25T22:02:43+00:00

In my MVC2 application I want most requests to use Forms authentication and requests

  • 0

In my MVC2 application I want most requests to use Forms authentication and requests to some specific URIs to use my custom authentication. In order to do so I added FormsAuthentication_OnAuthenticate() method and inside I check the URI and if it’s one of those exclusive URIs I want to check the username and password in the request headers.

The problem is how to terminate the request if the credentials provided are wrong.

I tried:

HttpContext context = args.Context;

context.Response.Write( "Wrong credentials" );
context.Response.StatusCode = 401;
context.Response.End();

but once that happens the request is forwarded to the URI that is specified in web.config under

<authentication mode="Forms">
      <forms loginUrl="~/LogOn"/>
</authentication>

I want the request to be terminated – so that the response is sent to the client and the connection is closed. How do I achieve that?

  • 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-05-25T22:02:43+00:00Added an answer on May 25, 2026 at 10:02 pm

    As long as you send 401 status code the Forms Authentication module intercepts this code and automatically redirects to the logon page. The correct way to handle authentication in ASP.NET MVC is using the [Authorize] attribute. And if you don’t want it to redirect to the login page but instead show some view you could write a custom authorize attribute and override the HandleUnauthorizedRequest method:

    public class MyAuthorizeAttribute : AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.Result = new ViewResult
            {
                ViewName = "~/Views/Shared/unauthorized.cshtml"
            };
        }
    }
    

    UPDATE:

    In addition to overriding the HandleUnauthorizedRequest you could override the AuthorizeCore method which allows you to perform custom authentication.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i m using .NET mvc2 for my application. I want some custom authorization on
I want to use System.Net.Mail.SendAync in an ASP.NET MVC2 application. I see that it
I am working on an MVC2 application and want to set the maxlength attributes
I'm using xval to use client side validation in my asp.net mvc2 web-application. Despite
I have an ASP.NET MVC2 application that uses a parent controller to setup specific
I am using ASP.NET MVC2 application in C#. I have two HTML forms on
I have asp.net MVC2 application. I am using VS2008 and want to hook up
I am trying to implement windows authentication in my ASP.NET MVC2 application. I've followed
I want to include some namespaces with their classes in my asp.net application. It
Hi i have mvc2 application where i want to stop processing application when settings

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.