Chris Tybur
Chris Tybur

Reputation: 1622

Basic auth in DNN Web API service

I'm building a library type module for DNN that will house a Web API service that is meant to be called by a separate application. It has one controller that inherits from DnnApiController. I'd like the requests in this service to use basic auth, since the other app has no association with DNN and its users won't be interacting with the portal. All it can do is pass in a username and password (this will happen over SSL). We are running DNN 7.3 which is configured to use standard Forms authentication.

Is it possible to configure just this service to use basic auth? If so, what attributes/configuration would I need to make it work?

Upvotes: 0

Views: 1910

Answers (1)

Fix It Scotty
Fix It Scotty

Reputation: 2852

I think you can do this with the DNNAuthorize attribute. First, I would add a role into DNN, example "ExternalApp". Then create a DNN user that has that role.

Make your web service code look like this:

public class MyAPIController : DnnApiController
{
    [HttpGet]
    [DnnAuthorize(StaticRoles="ExternalApp")]
    public string Ping()
    {
        return "MyAPI Version 01.00.00";
    }
}

Then in your external application (let's assume it is written in C#), you can do something like this:

string scheme = "https://";
string domainAlias = "www.website.com";
string modulePath = "myapimodule";
string controllerName = "myapi";
string apimethod = "ping";

Uri serviceUri = new Uri(string.Format("{0}{1}/DesktopModules/{2}/API/{3}/{4}", scheme, domainAlias, modulePath, controllerName, apimethod));
HttpWebRequest httpReq = (HttpWebRequest)HttpWebRequest.Create(serviceUri);
httpReq.Credentials = new NetworkCredential("externalappUser", "password123");
httpReq.Method = "GET";
httpReq.Accept = "application/text";

httpReq.BeginGetResponse(HttpWebRequestCallBack, httpReq);

Upvotes: 3

Related Questions