1

when I call my API Webservice its returns an empty array.

In my Header request, i have only a jwt token for authenticating

In Angular:

getSheets(): Observable<Sheet[]> {
return this.http.get(this.config.apiUrl + '/api/SheetsRelationAPI', this.jwt())
  .map(this.extractData)
  .do(data => console.log('SheetsData:', data))  // debug
  .catch(this.handleError);

In Asp.net MVC 5:

[HostAuthentication("bearer")]
[System.Web.Http.Authorize]
public class SheetsRelationAPIController : ApiController
{
    private GSheetsContext db = new GSheetsContext();

    // GET: api/SheetsRelation

    [ResponseType(typeof(SheetsRelationView))]
    public IQueryable<SheetsRelationView> GetSheetsRelation()
    {

        var claims = (User.Identity as System.Security.Claims.ClaimsIdentity).Claims;
        var username = "";
        foreach (var claim in claims)
            if (claim.Type.ToString() == "sub")
            {
                username = claim.Value.ToString();
            }
        //var tasks = from tsk in db.SheetsRelation.Include(s => s.SheetsContent.id  )
        //select tsk;

        var sheetsRelation = db.SheetsRelationView.Where(jt => jt.Username == username);




        return sheetsRelation;
    }
}

UPDATE 1:

It seems it's worked in PostMan and I have a JSON in response But in Angular, i haven't any JSON in response.

4
  • The claims seem to be emtpy. Have you debugged the controller to see what is going on? Commented Jul 10, 2017 at 13:16
  • i think this.http.get(this.config.apiUrl + '/api/SheetsRelationAPI', this.jwt()) should be this.http.get(this.config.apiUrl + '/api/SheetsRelationAPI', { headers: yourHeaders }. You have not specified what header you are sending in a key value pair. Commented Jul 10, 2017 at 13:22
  • @Hinrich The claims isn't empty, we test it with Postman and it successfully returns a JSON Commented Jul 11, 2017 at 4:37
  • @EkanshRastogi let me try your code , jwt() is a method that returns headers but I'm going to extract it and set it manually to see the result Commented Jul 11, 2017 at 4:40

1 Answer 1

1

Three things you may wish to try -

  1. Not related to this issue, but I always decorate my APIs with the specific http method, to ensure there isnt any confusion on my part - [HttpGet] in this case.

  2. Refactor your API class so that it doesnt have direct dependencies on GSheetsContext and User.Identity (make yourself an injectable service to handle that, so that you can mock the behavior.)

  3. Unit test the controller method with mocked dependencies, so that you can be sure that your controller is behaving as it is expected to.

Edit, if that sounds like too much work

Comment out your existing controller logic and put a stub method there that just returns something like return db.SheetsRelationView.Create()

If that works, then you know your issue is not with your API, and is in the logic. Then refer back to steps 2 & 3 above ;)

Sign up to request clarification or add additional context in comments.

3 Comments

I notice that if I calling the API outside of asp.net MVC Website domain, it works fine, but if I call it in the same domain like my angular project. it returns an empty JSON
Sounds like your claims are not being populated when calling from angular. Try adding logging in the method if you aren't able to debug it.
You could also consider returning Unauthorized() if there is no value found for your username claim.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.