2011-12-02 54 views
0

用户正在对REST WCF服务(我自己)进行身份验证。凭据通过AJAX以Javascript和JSON格式发送。服务回复一个确定的和很少的信息(重定向url)到客户端,当验证。FormsAuthenticate通过WCF(我如何使会话适用于浏览器?!)

现在,有一种为外部认证提供的新方法,并且我必须创建一个紧凑的代码片段,这些代码片段很容易粘贴到一个asp.net代码文件方法中运行的&。

一个典型的WCF请求最终可能会像这样,
http://testuri.org/WebService/AuthenticationService.svc/ExtLogin?cId=197&aId=someName&password=!!pwd

我的代码片段到目前为止,

protected void bn_Click(object sender, EventArgs e) 
{ 
    WebHttpBinding webHttpBinding = new WebHttpBinding(); 
    EndpointAddress endpointAddress = new EndpointAddress(url); 

    ContractDescription cd = 
     ContractDescription.GetContract(typeof(IAuthenticationService)); 

    ServiceEndpoint sep = new ServiceEndpoint(cd); 
    sep.Behaviors.Add(new WebHttpBehavior()); 
    sep.Address = endpointAddress; 
    sep.Binding = webHttpBinding; 

    var resp = new ChannelFactory<IAuthenticationService>(sepREST).CreateChannel(); 
    LoginResult result = resp.ExtLogin(cId, aId, hashPwd); 

    Response.Redirect(result.RedirectUri); 
    // I.e. http://testuri.org/Profile.aspx (Require authenticated to visit) 
} 

我收到的响应/结果对象正确验证答复。所以,沟通很好。当重定向到实际的网站时,我没有通过身份验证。我找不到问题?如果我使用上面的URI(使用有效的证书)并粘贴到我的Webbrowser URL中,然后手动键入uri,我就会进行身份验证。

我花了一天在网上搜索这个,没有成功。
有很多信息,但似乎没有适用。

我错过了什么?

我也尝试了另一种方法,但同样的问题仍然存在。

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uriWithParameters); 
CookieContainer cookieContainer = new CookieContainer(); 
request.CookieContainer = cookieContainer; 
request.ContentType = "application/json"; 
request.Accept = "application/json"; 
request.Method = "GET"; 

string result; 
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
    using (Stream stream = response.GetResponseStream()) 
     using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) 
      result = reader.ReadToEnd(); 

JavaScriptSerializer jsonDeserializer = new JavaScriptSerializer(); 
LoginResult contact = jsonDeserializer.Deserialize<LoginResult>(result);  
Response.Redirect(result.RedirectUri); 

回答

0

我不知道这个答案,但无论如何都会提供它作为其他人张贴:

我想这是因为已认证的要求是通过代码发送的请求。 当你重定向它是一个完全不同的请求 - 所以还没有通过身份验证。

所有身份验证技术都需要某种方式来维护“无状态”请求=会话Cookie或某种身份验证令牌的身份验证状态。

无论您从调用身份验证服务返回的令牌都需要可用于您的网站请求 - 将令牌从请求转储到Cookie可能是一种选择。

你能看到(像Fiddler这样的东西)auth令牌是作为'RedirectUrl'请求的一部分发送的吗?

+0

谢谢你的尝试。我想到了。当服务器端代码进行身份验证时,客户端(Web浏览器)不知道。如果我在url中发出uri请求,那么它是一个客户端请求。但是,如何让客户端了解服务器端身份验证? – Independent

+0

我*认为*您可以将cookie从HttpWebRequest请求中取出并保存到Web浏览器请求中 - 但我没有尝试过。 –

+0

这真的让我很难过。这个问题似乎更多地与HttpWebRequest获得他们自己的会话(并且像其他页面上的行为一样)相关。这意味着当我重定向时会话/ Cookies,来自另一个域的新会话尝试到达禁区。 – Independent

相关问题