2010-04-14 38 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();