I'm working with a WebApi project in C# (EF code first) and I'm using OData. I have a "User" model with Id, Name, LastName, Email, and Password.
In controller for example I have this code:
// GET: odata/Users
[EnableQuery]
public IQueryable<User> GetUsers()
{
return db.Users;
}
If I call /odata/Users I'll get all the data: Id, Name, LastName, Email, and Password.
How can I exclude Password from results but keep available in controller to make Linq queries?
How can I exclude Password from results but keep available in controller to make Linq queries?
Ignore it. From Security Guidance for ASP.NET Web API 2 OData:
There are two ways to exlude a property from the EDM. You can set the [IgnoreDataMember] attribute on the property in the model class:
public class Employee { public string Name { get; set; } public string Title { get; set; } [IgnoreDataMember] public decimal Salary { get; set; } // Not visible in the EDM }
You can also remove the property from the EDM programmatically:
var employees = modelBuilder.EntitySet<Employee>("Employees"); employees.EntityType.Ignore(emp => emp.Salary);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With