如何从android客户端应用程序中读取wcf服务中设置的身份验证cookie

时间:2013-05-08 07:17:47

标签: c# android wcf rest

您好我使用表单身份验证开发了Wcf服务,并且有一个登录服务方法来验证客户端。 此方法返回身份验证cookie。

HttpCookie cookie = null;
        returnValue = Membership.ValidateUser(userName, password);
        if (returnValue)
        {
            var ticket = new FormsAuthenticationTicket(
                    1,
                    userName,
                    DateTime.Now,
                    DateTime.Now.AddMinutes(5),
                    true,
                    userName // or user.UserID or other info you might find appropriate
                );
            string encryptedTicket = FormsAuthentication.Encrypt(ticket);
            cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            HttpContext.Current.Response.Cookies.Add(cookie);

        }
        return returnValue;

我想知道的是Android客户端如何调用此登录服务并获取cookie。

我知道如何使用c#作为bellow.But我想从Android Mobile中做同样的事情。所以请给出以下代码来自android版本的建议。

 var sharedCookie = string.Empty;
        bool isValid;
        string data = string.Empty;

        var authClient = new LoginServiceClient();
        using (new OperationContextScope(authClient.InnerChannel))
        {
            isValid = authClient.Login("admin1", "outreach#");
            if (isValid)
            {
                var response = (HttpResponseMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name];
                sharedCookie = response.Headers["Set-Cookie"];
            }
        }

        if (isValid)
        {

            var request = (HttpWebRequest)WebRequest.Create("http://localhost:48090/CustomService/GetAgencies");
            request.Headers["Cookie"] = sharedCookie;
            var responce = (HttpWebResponse)request.GetResponse();

            using (var stream = responce.GetResponseStream())
            {
                using (var reader = new StreamReader(stream))
                {
                    data = reader.ReadToEnd();
                }
            }
        }

如果您需要任何其他信息,请告知我们。

0 个答案:

没有答案
相关问题