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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:09:22+00:00 2026-06-15T03:09:22+00:00

I do have authorize attribute applied on my Web API. I am calling Web

  • 0

I do have authorize attribute applied on my Web API.
I am calling Web API from MVC4 application in which I am using standard cookie based authentication.
I need to call Web API method on controllers from integration tests but because authorize attribute is applied I will always receive unauthorized exception.

What is the best way to solve this problem ?
PS. I don’t want (need) to use other methods of authentication such as APIKey,Token in Auth Header and similar…

  • 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-15T03:09:24+00:00Added an answer on June 15, 2026 at 3:09 am

    First of all, one key element in order to answer this question is to know what kind of authentication mechanism you use. For example, if you use basic auth, you can send the credentials when you are integration testing:

    [Fact]
    public async Task FooTest() { 
    
        var username = "user";
        var password = "supersecret";
    
        // construct your config here as I do below.
        // RouteConfig and WebAPIConfig are my own classes
        var config = new HttpConfiguration();
        RouteConfig.RegisterRoutes(config);
        WebAPIConfig.Configure(config);
    
        var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/cars");
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
        request.Headers.Authorization = new AuthenticationHeaderValue(
            "Basic", EncodeToBase64(string.Format("{0}:{1}", username, password)));
    
        using (var httpServer = new HttpServer(config))
        using (var client = new HttpClient(httpServer)) {
    
            var response = await client.SendAsync(request);
            var result = await response.Content.ReadAsAsync<Car>();
    
            // do you test now...
        }
    }
    
    private static string EncodeToBase64(string value) {
    
        byte[] toEncodeAsBytes = Encoding.UTF8.GetBytes(value);
        return Convert.ToBase64String(toEncodeAsBytes);
    }
    

    Of course, your handler which handles the authentication should be able to authenticate you with those credentials.

    On the other hand, as you will be hosting the application in memory, setting an authenticated principal to the Thread.CurrentPrincipal would be another option but wouldn’t be my favorite option here.

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

Sidebar

Related Questions

I have an ASP.Net Web Forms application in which I'm using forms-based authentication with
I have a MVC Web Api Controller that uses the [Authorize] attribute at the
I have statically allowed access to controllers/action methods using the standard Authorize attribute with
On Friday I gave a demo of an MVC application using the Authorize attribute
I have an secure application with an Authorize attribute on each action. [Authorize(Roles =
I have a custom authorize attribute on my controllers and it is not being
I have an MVC site, secured using [Authorize] attributes, but have an issue on
I have a small application where I am creating a customer [Authorize] [AcceptVerbs(HttpVerbs.Post)] public
I have the following action method: [HttpGet, Authorize, OutputCache(Duration = 60, VaryByHeader = Cookie,
Currently, I'm trying to identify which Controller classes in my assembly have the [Authorize]

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.