Add ASP.NET Identity (User Management) to OData via Web API 2

I have an OData project that was created from a web API template (including credentials).

I have a class ApplicationUser: IdentityUser.

I have a TournamentContext: IdentityDbContext class.

I have a default AccountController that comes with a template with the [RoutePrefix ("api / Account")] attribute.

In WebApiConfig.cs For the default web template for the api template, I have

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new {id = RouteParameter.Optional});

To support OData, I added:

config.Routes.MapODataRoute("odata", "odata", GetModel(),
            new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));

and

private static IEdmModel GetModel()
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();            
        builder.EntitySet<Tournament>("Tournaments");
        return builder.GetEdmModel();
    }

Now, I want to expose account / user management through the OData API. How do I achieve this?

Thanks, Yaniv Ratson.

+3
source share
1 answer

ODataController , :

1) Controllers, Add → Controller...

2) " OData Web API 2 , Entity Framework"

3) - ApplicationUser (WebApplication1.Models) -

4) DbSet,

    public System.Data.Entity.DbSet<WebApplication1.Models.ApplicationUser> ApplicationUsers { get; set; }

    public DbSet<ApplicationUser> GetApplicationUsers()
    {
        return (DbSet<ApplicationUser>) Users;
    }

( IdentityDbContext, IDbSet Users). ,

. "ApplicationUsers" "Users" 'WebApplication1.Models.ApplicationUser'

.

5) last - "db.ApplicationUsers" "db.GetApplicationUsers()".

+3

All Articles