Windows Phone 7.1(Mango)上的表单身份验证?

3
我将翻译该段文字为:

我正在尝试使用WP7(Mango)模拟器通过表单认证登录到我的工作网站。(最终创建一个WP7应用程序,让您在该网站上查看某些内容)

该网站的Web配置包含以下内容:

  <system.web.extensions>
<scripting>
    <webServices>
        <authenticationService enabled="true" requireSSL="false"/>
    </webServices> 
</scripting>

他们有一份示例代码,基本上通过调用身份验证 ODataService 登录,传递用户名和密码并获取身份验证 cookie。

使用此 cookie,所有新请求都将包括它发送到 ODataService,并应该能够检索数据。

在进行任何请求之前 - 获取身份验证 cookie

private static string GetAuthenticationCookie(string username, string password)
    {
        if (authenticationCookie == null)
        {
            string loginServiceAddress = websiteUrl + "/Authentication_JSON_AppService.axd/Login";

            var request = CreateAuthenticationRequest(username, password, loginServiceAddress);

            var response = request.GetResponse();
            if (response.Headers["Set-Cookie"] != null)
            {
                authenticationCookie = response.Headers["Set-Cookie"];
            }
            else
            {
                throw new SecurityException("Invalid credentials");
            }
        }

        return authenticationCookie;
    }       

private static WebRequest CreateAuthenticationRequest(string username, string password, string loginServiceAddress)
    {
        var request = HttpWebRequest.Create(loginServiceAddress);

        request.Method = "POST";
        request.ContentType = "application/json";
        var body = string.Format(
            "{{ \"userName\": \"{0}\", \"password\": \"{1}\", \"createPersistentCookie\":false}}",
            username,
            password);
        request.ContentLength = body.Length;

        var stream = request.GetRequestStream();
        using (var sw = new StreamWriter(stream))
        {
            sw.Write(body);
        }

        return request;
    }

我不能直接将此代码复制粘贴到 Windows Phone 7.1(Mango) 应用程序中,因为网络代码有所不同——例如在 WP7 中,所有操作都是异步完成,而类似于

的内容也需要修改。

myWebRequest.GetResponse()

不再起作用了...我需要做一些类似的事情

myWebRequest.BeginGetResponse()

如果有任何使用表单身份验证在WP7上进行身份验证的工作代码示例,那就太好了。非常感谢。
1个回答

2

请查看这个http类:

http://mytoolkit.codeplex.com/wikipage?title=Http

下面的代码应该可以解决问题:

private void OnButtonClick(object sender, RoutedEventArgs e)
{
    const string username = "user";
    const string password = "password";
    const string loginServiceAddress = "http://www.server.com/Services/AuthenticationService.svc";

    var text = string.Format("{{ \"userName\": \"{0}\", \"password\": \"{1}\", \"createPersistentCookie\":false}}", 
        username, password);

    var request = new HttpPostRequest(loginServiceAddress);
    request.RawData = Encoding.UTF8.GetBytes(text);
    request.ContentType = "application/json";
    Http.Post(request, AuthenticationCompleted);
}

private void AuthenticationCompleted(HttpResponse authResponse)
{
    const string serviceAddress = "http://www.server.com";
    if (authResponse.Successful)
    {
        var sessionCookie = authResponse.Cookies.Single(c => c.Name == "SessionId");
        var request = new HttpGetRequest(serviceAddress);
        request.Cookies.Add(new Cookie("SessionId", sessionCookie.Value));
        Http.Get(request, OperationCallCompleted);
    }
}

private void OperationCallCompleted(HttpResponse response)
{
    if (response.Successful)
    {
        // TODO: do your parsing
        var responseText = response.Response;

        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            // TODO: set data to binding or directly to user control (in UI thread)
        });
    }
}

1
我无法使用该代码,编译错误 - 一些类缺失,HttpUtilityExtensions 似乎是罪魁祸首 - 尽管如此,我会尝试解决它。 - Jerrold
2
抱歉,HttpUtilityExtensions可以在此处找到:https://mytoolkit.svn.codeplex.com/svn/Utilities/HttpUtilityExtensions.cs - Rico Suter
1
编辑:https://mytoolkit.svn.codeplex.com/svn/Shared/Utilities/HttpUtilityExtensions.cs - Rico Suter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接