How to use a MVC WebAPI OData endpoint securely?

后端 未结 2 1126
别跟我提以往
别跟我提以往 2020-12-22 03:50

I have an OData endpoint defined at ~/odata/, which doesn\'t need to be accessed unless a user has been authenticated (in fact, how would you secure this for no

2条回答
  •  萌比男神i
    2020-12-22 04:26

    So the major hurdle to get past is thinking that all WebAPI requests (using the OData syntax) are stateless. Of course, in a stateless environment this makes this more difficult.

    However, with the WebAPI endpoint secured through web.config requiring an authenticated (stateful) request, we should be able to grab the UserName (or UserID or any other custom property when using a custom membership provider), by something like var userId = ((CustomIdentity)HttpContext.Current.User.Identity).UserId.

    Once this is established, we will need to add something like "WHERE UserID = userId;" before the request is issued:

            var unitOfWork = new Repository.UnitOfWork(_db);
    
            var users = options.ApplyTo(unitOfWork.Repository().Queryable
                .Include(w => w.NavigationProperty1)
                .Where(u => u.UserId == UserContext.Identity.UserId)
                .OrderBy(o => o.SomeProperty))
                .Cast().ToList();
    

    Additional suggestions welcome.

提交回复
热议问题