简体   繁体   English

如何从.NET客户端上的ASP.NET MVC5 +身份服务器接收身份验证失败?

[英]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: 我提出了一个中间解决方案:

  1. 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> 
  2. In .NET client check content string for the specific tag presence. 在.NET客户端中,检查内容字符串是否存在特定的标记。

I don't think this the preformance-wise solution but at least it works... 我不认为这是出色的解决方案,但至少它能起作用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从服务器卸载SSL时ASP.NET OWIN身份错误AAD身份验证失败 - ASP.NET OWIN Identity Error AAD Auth Failure when SSL is Offloaded From Server 如何让asp.net mvc应用程序从客户端接收简单的文本字符串数据? - How to have an asp.net mvc app receive simple text string data from a client? 如何在ASP.NET MVC中从POST接收数据 - How receive data from POST in asp.net MVC Asp.net MVC-从服务器推送延迟的客户端消息 - Asp.net MVC - Push a deferred client message from server 从Android客户端轮询asp.net MVC服务器 - Polling asp.net MVC server from an Android client ASP.NET 使用 Identity Server JWT 和 Auth Cookie 进行核心身份验证 - ASP.NET Core authenticating with Identity Server JWT and Auth Cookie ASP.NET MVC如何将应用程序从ASP.NET身份转换为Azure AD - ASP.NET MVC How to convert application from ASP.NET Identity to Azure AD 模型如何在Asp.Net MVC3中将数据从客户端传递到服务器? - How Model passes data from client to server in Asp.Net MVC3? 如何将MySql与asp.net身份一起使用以及将MVC与Microsoft ASP.NET身份示例一起使用? - How to use MySql with asp.net identity and MVC with Microsoft ASP.NET Identity Samples? 如何接收和保存从客户端(使用Ajax进行J查询)发送到服务器端(ASP.net C#)的数据? - How to receive and save the data sent from client side (J-query using Ajax) to server side (ASP.net C#)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM