我正在尝试使用表单例份验证使用 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/