我如何...从Forms身份验证的ASP.NET站点调用Windows身份验证的RESTful WCF [英] How do I...Call Windows authenticated RESTful WCF from Forms authenticated ASP.NET site

查看:67
本文介绍了我如何...从Forms身份验证的ASP.NET站点调用Windows身份验证的RESTful WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用基于表单的身份验证的ASP.NET网站,并验证来自Active Directory的用户。我有一个单独的Restful WCF服务,Windows身份验证。以下是其终点:

I have a ASP.NET website using Form Based Authentication and validates users from Active Directory. I have a separate Restful WCF service that is Windows Authenticated. Below is its endpoint:

<bindings>
  <webhttpbinding>
    <binding name="RestBinding" maxreceivedmessagesize="2147483647">
             maxBufferPoolSize="2147483647"&gt;
      <readerquotas maxdepth="2147483647" maxarraylength="2147483647">
                    maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"
                    maxStringContentLength="2147483647" /&gt;
      <security mode="Transport">
        <transport clientcredentialtype="Windows" />
      </security>
    </readerquotas></binding>
  </webhttpbinding>
</bindings>



两个网站都需要在HTTPS上。我正在从ASP.NET站点跨域调用REST服务。如果服务是匿名和HTTP,一切都很好。我将SSL绑定到Service并在其上启用Windows身份验证的那一刻,我开始在ASP.NET站点中获得401身份验证失败。



我甚至尝试将FBA用户名传递给WCF服务和强制登录但它不起作用。请参阅以下代码:


Both sites need to be on HTTPS. I am making cross domain call to REST service from ASP.NET site. Everything is working great if the service is Anonymous and on HTTP. The moment I bind SSL to Service and enable Windows authentication on it, I start getting 401 Authentication failure in ASP.NET site.

I even tried passing the FBA username to WCF service and forcing login but it did not work. See code below:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
  HttpContext.Current.User = new WindowsPrincipal(new
        WindowsIdentity(Request["Login_User"].ToString()));
}



我有CORS标题:


And I have the CORS headers in place:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (Request.ServerVariables["HTTP_ORIGIN"] != null)
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", Request.ServerVariables["HTTP_ORIGIN"]);
    else if (Request.UrlReferrer != null)
    {
        Uri urlOrgin = new Uri(Request.UrlReferrer.AbsoluteUri);

        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", urlOrgin.ToString().Replace(urlOrgin.PathAndQuery, string.Empty));
    }
    else
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true");

    if (HttpContext.Current.Request.HttpMethod.ToUpper() == "OPTIONS")
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE, GET, OPTIONS");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "WWW-Authenticate");
        HttpContext.Current.Response.End();
    }
}



查看我的jquery电话:


Look at my jquery call:

$.ajax({
  type: "GET",
  url: surl,
  dataType: "json",
  contentType: 'application/json; charset=utf-8',
  data: JSON.stringify({ Login_User: "xxxx.xxxx" }),
  xhrFields: {
    withCredentials: true
  },
  success: function (data) {
    alert(JSON.stringify(data));
  },
  error: function (a) {
    alert(JSON.stringify(a));
  }
});



我甚至尝试过NTLM.js但没有结果。请帮助解决这个问题。


I even tried the NTLM.js but no result. Please help get past this.

推荐答案

.ajax({
type: GET
url:surl,
dataType: json
contentType:' application / json; charset = utf- 8'
数据: JSON .stringify({Login_User: < span class =code-string> xxxx.xxxx}),
xhrFields:{
withCredentials: true
},
成功:功能(数据){
alert( JSON .stringify(data));
},
错误: function (a){
alert( JSON .stringify(a));
}
});
.ajax({ type: "GET", url: surl, dataType: "json", contentType: 'application/json; charset=utf-8', data: JSON.stringify({ Login_User: "xxxx.xxxx" }), xhrFields: { withCredentials: true }, success: function (data) { alert(JSON.stringify(data)); }, error: function (a) { alert(JSON.stringify(a)); } });



我甚至试过N TLM.js但没有结果。请帮忙解决这个问题。


I even tried the NTLM.js but no result. Please help get past this.


通过这个: http://stackoverflow.com/questions/22460150/401-client-negotiate-server-negotiate-ntlm-when-calling-wcf-server-to-serve [ ^ ]







http://forums.asp.net/t/1165237 .aspx?+远程+服务器+返回+ +错误+ 401 +未授权+ ASP + net2005 + [ ^ ]
Go through this : http://stackoverflow.com/questions/22460150/401-client-negotiate-server-negotiate-ntlm-when-calling-wcf-server-to-serve[^]

and

http://forums.asp.net/t/1165237.aspx?The+remote+server+returned+an+error+401+Unauthorized+ASP+net2005+[^]


这篇关于我如何...从Forms身份验证的ASP.NET站点调用Windows身份验证的RESTful WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆