My boss asked me to add the ability to insert new jobs using the jobs oData service I wrote about a few posts ago.
First, add the ability to write data:
public static void InitializeService(DataServiceConfiguration config) { config.SetEntitySetPageSize("*", 50); // limit to 50 rows config.SetEntitySetAccessRule("Jobs", EntitySetRights.AllRead | EntitySetRights.WriteAppend | EntitySetRights.ReadSingle); ...
Again, the only problem with this is that anyone on the internet can access this and write data.
So we need to add a ChangeInterceptor to make sure the user is authenticated:
[ChangeInterceptor("Jobs")] public void OnChangeEntries(Job job, UpdateOperations operations) { if (!HttpContext.Current.Request.IsAuthenticated) { throw new DataServiceException("You must be authenticated to create a new Job."); } }
And that’s it, all done.