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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:57:16+00:00 2026-06-12T07:57:16+00:00

I am using ASP.NET MVC 4 with Web Api I have the following ApiController.

  • 0

I am using ASP.NET MVC 4 with Web Api

I have the following ApiController.

public class ProductsController : ApiController
{
    public List<Product> GetProducts()
    {
        return _productService.GetAllProducts();
    }

    public List<Product> GetProductsFromId(string username)
    {
        return _productService.GetProductsFromUsername(username);
    }
}

Now if you see the second Action GetProductsFromId(string username) here I need to pass username which earlier (i.e before upgrading from MVC 3) I was using User.Identity.Username to get the username.

How should I handle this in such a scenario.

How do I pass the username ? Is there something I could do.

Also the I dont want to use User.Identity.Username inside the GetProductFromId(string username) method as it will defeat the entire purpose of using web api and will not be testable too.

Please guide me help me on this. Thanks

  • 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-12T07:57:17+00:00Added an answer on June 12, 2026 at 7:57 am

    Also the I dont want to use User.Identity.Username inside the
    GetProductFromId(string username) method as it will defeat the entire
    purpose of using web api and will not be testable too.

    That’s exactly what you should use:

    [Authorize]
    public List<Product> GetProductsFromId()
    {
        string username = User.Identity.Name;
        return _productService.GetProductsFromUsername(username);
    }
    

    Notice the [Authorize] attribute. Depending on the authorization scheme you are using the User.Identity will be populated differently. For example if you have enabled forms authentication then the username will obviously come from the forms authentication cookie that the client need to pass when invoking the action. You could also write a custom handler for example if you are using basic authentication instead of forms authentication. I wrote an example here.

    This doesn’t defeat any purpose of unit testing. This method is perfectly fine unit testable. The User property of the ApiController is an IPrincipal which could be trivially mocked in a unit test. For example with Moq:

    // arrange
    var sut = new ProductsController();
    var user = new Mock<IPrincipal>();
    var identity = new Mock<IIdentity>();
    user.Setup(x => x.Identity).Returns(identity.Object);
    identity.Setup(x => x.Name).Returns("john");
    Thread.CurrentPrincipal = user.Object;
    
    // act
    var actual = sut.GetProductsFromId();
    
    // assert
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using web api with ASP.NET MVC 4. I have the following named
I have an ASP.NET MVC 4 Web API app using EntityFramework for ORM. In
I'm using ASP.net MVC C# in Visual Studio Web Dev. I have a couple
I'm implementing a web API using the REST for ASP.NET MVC framework (MVC 2).
I am using ASP.NET MVC 4 Beta with Web API - although I believe
I have a web application written in ASP .NET MVC 3. I'm using ACS
I am developing some sort of service using asp.net mvc 4 web api. On
I am new to ASP.Net MVC and ASP.Net web api. I have worked on
I am using ASP.Net mvc 3 C# to make a web application. I have
I have created a sample project using ASP.NET MVC 3 Web Application (Razor) template.

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.