[英]How to receive auth failure from ASP.NET MVC5+Identity server on .NET client?
i'm using ASP.NET MVC5 with the latest Identity and Signalr on the server and have .NET client app. 我在服务器上使用具有最新Identity和Signalr的ASP.NET MVC5,并具有.NET客户端应用程序。 Currently i have working auth logic implemented but i don't get it how can i get auth failure in .NET desktop client ?
目前,我已经实现了有效的身份验证逻辑,但是我不明白如何在.NET桌面客户端中获得身份验证失败?
Here is my .NET desktop client auth code: 这是我的.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;
}
}
where _connection is a hub connection which receives cookie needed for hub auth. 其中_connection是一个集线器连接,它接收集线器身份验证所需的cookie。 The problem is that httpCLient.PostAsync() always return valid result and i don't get it how i can implement auth failure detection.
问题是httpCLient.PostAsync()始终返回有效结果,但我不知道如何实现身份验证失败检测。
Here is server login code: 这是服务器登录代码:
// 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);
}
On failure it just adds error string on the page. 如果失败,它只会在页面上添加错误字符串。
Please advice what is the better way to implement auth result. 请告知实现身份验证结果的更好方法是什么。
This is strange that i got no single answer for this question. 奇怪的是我没有一个答案。 I've come to an intermediate solution:
我提出了一个中间解决方案:
Add unique hidden tags for login and index pages (on failure login page is displayed again, on success - index page) 为登录和索引页面添加唯一的隐藏标签(在失败登录页面上再次显示,在成功页面上-索引页面)
<div style="display: none;" id="@SharedData.Constants.INDEX_PAGE_TAG"></div>
I don't think this the preformance-wise solution but at least it works... 我不认为这是出色的解决方案,但至少它能起作用...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.