0
我正在使用ASP.NET MVC5與服務器上的最新身份和信號,並有.NET客戶端應用程序。目前我有工作認證邏輯實施,但我不明白如何才能得到認證失敗在.NET桌面客戶端?如何在.NET客戶端上從ASP.NET MVC5 +身份服務器接收身份驗證失敗?
這是我的.NET桌面客戶端身份驗證碼:
private static async Task<bool> AuthenticateUser(string siteUrl, string email, string password)
{
try
{
var handler = new HttpClientHandler { CookieContainer = new CookieContainer() };
using (var httpClient = new HttpClient(handler))
{
var loginUrl = siteUrl + "Account/Login";
_writer.WriteLine("Sending http GET to {0}", loginUrl);
var response = await httpClient.GetAsync(loginUrl);
var content = await response.Content.ReadAsStringAsync();
_verificationToken = ParseRequestVerificationToken(content);
content = _verificationToken + "&UserName="+email+"&Password="+password+"&RememberMe=false";
_writer.WriteLine("Sending http POST to {0}", loginUrl);
response = await httpClient.PostAsync(loginUrl, new StringContent(content, Encoding.UTF8, "application/x-www-form-urlencoded"));
content = await response.Content.ReadAsStringAsync();
_verificationToken = ParseRequestVerificationToken(content);
_connection.CookieContainer = handler.CookieContainer;
return true;
}
}
catch (Exception ex)
{
Logger.Log(ex, "Auth");
return false;
}
}
其中_connection是接收所需轂AUTH餅乾的輪轂連接。問題是,httpCLient.PostAsync()總是返回有效的結果,我不明白如何我可以實現身份驗證失敗檢測。
這裏是服務器的登錄代碼:
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
如果失敗,它只是增加了錯誤字符串在頁面上。
請指教什麼是更好的方式來實現auth結果。