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

  • Home
  • SEARCH
  • 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 7760387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:52:26+00:00 2026-06-01T13:52:26+00:00

I have the following action in my ASP.NET Web API: public IEnumerable<Car> carssOfUser(int id,

  • 0

I have the following action in my ASP.NET Web API:

    public IEnumerable<Car> carssOfUser(int id, String username, String password)
    {

        using (var context = new CarEntities())
        {
            // Authorization. Allow user only to get his own cars
            User userQueried = context.Users.Where(u => u.id == id).FirstOrDefault();
            if (!userQueried.username.Equals(username))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
                //return null;
            }

            IEnumerable<Car> cars = context.Cars.Where(p => p.ownerId == id).ToList();
            return cars;
        }
    }

However, if I query it from Android application to obtain cars of given users (through json).. let’s say with id 2, it returns collection of 9 cars (that’s correct, user 2 has 9 cars), but only first car has proper field values (name, price etc.), the rest of cars have null in all fields.

That’s really strange, cause the problem happened when I added these lines of code:

// Authorization. Allow user only to get his own cars
            User userQueried = context.Users.Where(u => u.id == id).FirstOrDefault();
            if (!userQueried.username.Equals(username))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
                //return null;
            }

After removing it, everything works perfectly fines and all cars have proper values in fields (not nulls anymore)…

Do you have any idea how to explain this imo very strange behavior?

I don’t think it matters (cause all other actions where I don’t have this kind of authorization works without problems), but I want to mention that from Android app I use gson to deserialize, and on server side I use JSON.NET to serialize.

  • 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-01T13:52:29+00:00Added an answer on June 1, 2026 at 1:52 pm

    I looked at the database access code and I put authorization code into using statement:

        using (var context = new GeoDiaryEntities())
        {
            // Authorization. Allow user only to get his own travels
            User userQueried = context.Users.Where(u => u.id == id).FirstOrDefault();
            if (!userQueried.username.Equals(username))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }
        }
    

    It helped and it works now.
    However, I’m not really sure why executing this code within separate context helped..
    any ideas?

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

Sidebar

Related Questions

I have a controller with the following action code: public JsonResult CheckPasswordStrength(string password, string
i have the following model method inside my asp.net MVc web application:- public IQueryable<User>
On ASP.NET MVC 3, assume that we have following controller action: public ActionResult Index()
I have the following DeletePOST action method:- [HttpPost] public ActionResult Delete(int id) { try
This is for an ASP.NET MVC3 web application. In RegisterRoutes I have the following
I have the following JMenu inside my asp.net mvc3 web application:- <ul id=jMenu> <li><a
I have following code: public static void ProcessStep(Action action) { //do something here if
I have an Asp.Net MVC 2 web application deployed on IIS 7.5 on .Net
I have an ASP.NET MVC app with the following deployment requirements: The URL structure
We have an ASP.NET MVC web app which allows users to publish messages onto

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.