我有一个简短的问题,请求大家尽快回复。
我开发了一个基于表单的身份验证的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.从webApplication我通过在添加上述web服务的webreferance后创建代理来调用webservice。
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应用程序中使用它时,从webservice代理抛出以下异常。
<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>
你能否分享任何想法来解决它?
答案 0 :(得分:5)
您需要设置
service.AllowAutoRedirect = true
如果您打算在代码中重定向。
答案 1 :(得分:0)
试过这个并且工作:转到您在IIS中托管Web服务的网站,单击会话状态,将Cookie设置的模式更改为使用Cookie。完成。
答案 2 :(得分:0)
您需要同时设置
service.AllowAutoRedirect = true;
service.CookieContainer = new CookieContainer();