Blogs

The One With

OData and OAuth - Part 1 – Introduction

     

I often receive questions about OData and authentication. Specifically, about OData + OAuth. The scenarios are very simple, you have a site/service and you want to provide developer API so that third-parties can integrate. The API must of course be secure, callers must properly be authenticated and only authorized resources/data must be available.

Before we start going into details,  let’s build a sample site just so we have some context.

image

Our site we’ll be a front-end for the Hobbit's Bank Of The Shire Smile and we’ll use:

The Site

image

The site would let members sing-in by presenting a list of all members, and a member would than click on himself to sign-in.

image

Members will be stored in the Principals table and represented by the XPO object Principal:

[Persistent("Principals")]
public class Principal : XPLiteObject {
    public Principal() { }
    public Principal(Session session) : base(session) { }

    [Key(AutoGenerate = true)]
    public Guid ID { get; set; }

    [Size(128)]
    public string FullName { get; set; }

    public byte[] Picture { get; set; }
    
    [Size(128)]
    [Indexed(Unique=true)]
    public string Email { get; set; }
    
    public DateTime Created { get; set; }
    public DateTime Modified { get; set; }
}

Once signed-in, a member would be able to see all the valuables in his safety-deposit box:

image

Valuables will be stored in the Valuables table and represented by XPO object Valuable:

[Persistent("Valuables")]
public class Valuable : XPLiteObject {
    public Valuable() { }
    public Valuable(Session session) : base(session) { }
    
    [Key(AutoGenerate = true)]
    public Guid ID { get; set; }

    [Size(128)]
    public string Name { get; set; }

    [Indexed(Unique=false)]
    public Guid Owner { get; set; }
}

The API

Our data will be exposed using WCF Data Services using the XPO Data Service Provider:

[ConnectionString("BankOfShire")]
public class OData : XpoDataService {
    public static void InitializeService(DataServiceConfiguration config) {
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}

Web.config

  <connectionStrings>
    <add name="BankOfShire" 
         connectionString="XpoProvider=MySql;Server=localhost;Database=BankOfShire;Uid=root;Pwd=password;" />
  </connectionStrings>

XPO Data Service Provider lives in DevExpress.Xpo.v10.2.Data.Services so we’ll to reference that. And because we are using MySQL we’ll need:

We can now make some OData calls from the browser:

image

Now that the foundation is laid in here is what to expect from this series:

Download the source code for Part 1

Cheers
Azret

Published Feb 24 2011, 01:48 AM by Azret Botash (DevExpress)
Filed under: , , , ,
Technorati tags: OAuth, MVC, MySQL, XPO, OData
Bookmark and Share

Comments

 

Gary L Cox Jr [DX-Squad] said:

I'm interested in seeing how you secure the OData feeds without query interceptors.

February 24, 2011 11:08 AM
 

Gabriel Santillan Mass said:

This works with ASP.Net, not ASP.net MVC?

March 17, 2011 12:00 PM
 

Azret Botash (DevExpress) said:

Gabriel, the sample app is ASP.NET MVC 3 but everything applies to ASP.NET WebForms apps as well...

March 17, 2011 7:53 PM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.