Inserting with OData

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s