I want to build a SPA module. I used Chris Hammond's "DNN (DotNetNuke) 8/9 C# DAL2 SPA Module" template in Visual Studio 2017. I have a local instance of DNN 9 setup.
I created a new project in the DesktopModules folder called ProductModule. In the services folder, I created a new controller called ProductsController.cs:
using System.Web.Http;
using ProductsProductModule.Components;
using ProductsProductModule.Services.ViewModels;
using DotNetNuke.Common;
using DotNetNuke.Web.Api;
using DotNetNuke.Security;
using System.Threading;
using DotNetNuke.UI.Modules;
using DotNetNuke.Common.Utilities;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace ProductsProductModule.Services
{
[SupportedModules("ProductModule")]
//[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
[AllowAnonymous]
public class ProductsController : DnnApiController
{
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
[AllowAnonymous]
[HttpGet]
public IEnumerable GetAllProducts()
{
return products;
}
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
}
I modified the View.html file like this:
[JavaScript:{ jsname: "JQuery" }]
[JavaScript:{ jsname: "Knockout" }]
[JavaScript:{ path: "~/Resources/Shared/scripts/dnn.jquery.js"}]
Test Web Api
I compiled the module and installed the extension in my local DNN instance. When I go to dnndev.me/DesktopModules/ProductModule/Api/Products/GetProducts, I get an error message:
Authorization has been denied for this request.
Adding the module to the page shows:
Test Web Api
Unauthorized
What am I missing? Are there headers I need to include when sending the request? Doesn't seem to be enough documentation on how to use the authentication and authorization components.
Thank you for any help you can provide.