gpt4 book ai didi

asp.net - Windows Phone 7.1 (Mango) 上的表单例份验证?

转载 作者:行者123 更新时间:2023-12-02 02:21:11 24 4
gpt4 key购买 nike

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

站点 webconfig 包含以下内容:

  <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 中使用表单验证进行身份验证的任何工作代码示例,那就太好了

非常感谢

最佳答案

检查这个 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)
});
}
}

关于asp.net - Windows Phone 7.1 (Mango) 上的表单例份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8083831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com