如何显示登录失败的错误消息 [英] How to show error message for login fail

查看:152
本文介绍了如何显示登录失败的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试执行抛出错误的代码,如果用户在3次尝试后使用以下代码无法登录:





I am trying the execute the code which throw's an error, if user fails to login after 3 attempts using the following code below:

private Task<HttpResponseMessage> Unauthorized(HttpRequestMessage request)
        {
            var response = request.CreateResponse(HttpStatusCode.Unauthorized);
            response.Headers.Add(BasicAuthResponseHeader, BasicAuthResponseHeaderValue);
            var task = new TaskCompletionSource<HttpResponseMessage>();
            task.SetResult(response);
            return task.Task;
        }

        private api_login ParseAuthorizationHeader(string authHeader)
        {
            string[] credentials = Encoding.ASCII.GetString(Convert.FromBase64String(authHeader)).Split(new[] { ':' });
            if (credentials.Length != 2 || string.IsNullOrEmpty(credentials[0]) || string.IsNullOrEmpty(credentials[1])) return null;

            return new api_login()
            {
                username = credentials[0],
                password = credentials[1],
            };
        }

        private bool TryGetPrincipal(string userName, string password, out IPrincipal principal)
        {

            // data access in a repository or separate layer/library.
            api_login user = repository.Validate2(userName, password);

            int failedAttempts = 0;
            if (user.username != null)
            {
                // once the user is verified, assign it to an IPrincipal with the identity name and applicable roles
                failedAttempts += 1;
                principal = new GenericPrincipal(new GenericIdentity(user.username), null);
            }

            else if (failedAttempts == 3)
            {
                //error message code
               
            }
            principal = null;

            return false;
        }
           
    }





**更新代码**



** Updated Code **

else if (user.username == null)
            {
                //throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, String.Format("this identity does not exist")));
                throw new HttpResponseException(HttpStatusCode.NotFound);
               
            }





我尝试添加HttpResponseException但我似乎无法在客户端收到错误消息 - 当我输入错误的用户凭据时,除了空白屏幕外,它结束。请帮忙。



提前致谢。



I tried adding "HttpResponseException" but I cannot seem to get error message on the client-end, except its a blank screen, when I type in the incorrect user credentials. Please help.

Thanks in advance.

推荐答案

ScriptManager.RegisterStartupScript(this,GetType(),"showalert","alert('Only alert Message');",true);


这篇关于如何显示登录失败的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆