Basic auth in DNN Web API service

拥有回忆 提交于 2020-01-25 08:25:06

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/34028517/basic-auth-in-dnn-web-api-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!