2010-04-14 31 views
3

我有一个简短的问题,并要求大家尽快作出回应。对象在使用web服务时移动错误

我已经开发了一个基于表单的身份验证的Web服务,如下所示。

1.在web.config中输入如下。

<authentication mode="Forms"> 
    <forms loginUrl="Loginpage.aspx" name=".AuthAspx"></forms> 
</authentication> 
<authorization> 
    <deny users="?"/> 
</authorization> 
<authentication mode="Forms"> 
    <forms loginUrl="Loginpage.aspx" name=".AuthAspx"/> 
</authentication> 
<authorization> 
    <deny users="?"/> 
</authorization> 

2.在登录页面用户在按钮点击事件上验证如下。

if (txtUserName.Text == "test" && txtPassword.Text == "test") 
{ 
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, // Ticket version 
      txtUserName.Text,// Username to be associated with this ticket 
      DateTime.Now, // Date/time ticket was issued 
      DateTime.Now.AddMinutes(50), // Date and time the cookie will expire 
      false, // if user has chcked rememebr me then create persistent cookie 
      "", // store the user data, in this case roles of the user 
      FormsAuthentication.FormsCookiePath); // Cookie path specified in the web.config file in <Forms> tag if any. 

    string hashCookies = FormsAuthentication.Encrypt(ticket); 

    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies); // Hashed ticket 

    Response.Cookies.Add(cookie); 

    string returnUrl = Request.QueryString["ReturnUrl"]; 

    if (returnUrl == null) returnUrl = "~/Default.aspx"; 

    Response.Redirect(returnUrl); 
} 

3.Webservice有一个默认的webmethod。

[WebMethod] 
public string HelloWorld() 
{  
    return "Hello World";    
} 

4.从web应用,我通过将上述web服务的webreferance后产生的代理进行到web服务的调用。

localhost.Service1 service = new localhost.Service1(); 

service.AllowAutoRedirect = false; 

NetworkCredential credentials = new NetworkCredential("test", "test"); 

service.Credentials = credentials; 

string hello = service.HelloWorld(); 

Response.Write(hello); 

并且在Web应用程序中使用它时,以下异常从web服务代理中抛出。

<html><head><title>Object moved</title></head><body> 
<h2>Object moved to <a href="%2fWebService1%2fLoginpage.aspx%3fReturnUrl %3d%252fWebService1%252fService1.asmx">here</a>.</h2> 
</body></html> 

请问您能分享一些想法来解决它吗?

回答

5

您需要设置

service.AllowAutoRedirect = true 

如果你计划在你的代码重定向。

+0

我试过,但它抛出一个异常 - 客户端发现“text/html的响应内容类型; charset = utf-8',但预计'text/xml'。通过显示登录页面的视图源。 – NandaGopal 2010-04-14 13:24:40

+1

将其重定向到登录页面。 – Nix 2010-04-14 14:21:51

0

刚刚尝试过这个工作:转到您在IIS中托管Web服务的网站,单击会话状态,将Cookie设置的模式更改为使用Cookie。完成。

0

您需要同时设置

service.AllowAutoRedirect = true; 
service.CookieContainer = new CookieContainer();