0

嗨,我使用表单身份验证开发了一个 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# 来消费。但我想从 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();
                }
            }
        }

如果您需要更多信息,请告诉我们。

4

0 回答 0