开发者

WCF. REST architecture + basic authentication. .NET 4

开发者 https://www.devze.com 2023-03-26 00:35 出处:网络
I need an advice from people who already used base authentication in WCF Service. I found a lot of examples how to do it like:

I need an advice from people who already used base authentication in WCF Service.

I found a lot of examples how to do it like:

http://www.codeproject.com/KB/WCF/BasicAuthWCFRest.aspx

and WcfRestContrib. But first example for .NET 3.5 a开发者_开发百科nd second is not supported by anymore by author. Maybe in .NET 4 things a better and we have some functionality that can help me with this issue? Will appreciate any clues and information.

And for some reason i even cannot get data from secured server because of strange parse error when trying to get data with JQuery. Without basic authentication everything working fine and with json cannot be parsed. Maybe somebody meet such problem.


I think you have a problem with the OPTIONS call which is issued when using basic authentication in jquery. You can solve this by adding an global.asax to your project and add the following code:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    EnableCrossDomainAjaxCall();
}

private void EnableCrossDomainAjaxCall()
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept");
        HttpContext.Current.Response.End();
    }
}

You can also download an example solution here: http://sameproblemmorecode.blogspot.com/2011/10/creating-secure-restfull-wcf-service.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号