在Windows Phone 7.1(芒果)上进行表单身份验证?

时间:2011-11-10 17:36:03

标签: asp.net asp.net-membership forms-authentication windows-phone-7.1 windows-phone-7

我正在尝试使用表单身份验证使用WP7(Mango)模拟器登录我的作品网站。 (最终创建一个WP7应用程序,让您查看此站点上的一些内容)

网站webconfig包含:

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

他们有示例代码,基本上通过调用身份验证ODataService登录,传入user / pass并获取身份验证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中使用表单auth进行身份验证的任何工作代码示例 - 这将是很好的

非常感谢

1 个答案:

答案 0 :(得分: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)
        });
    }
}
相关问题