Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi,

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:

C#
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 **
C#
else if (user.username == null)
            {
                //throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, String.Format("this identity does not exist")));
                throw new HttpResponseException(HttpStatusCode.NotFound);
               
            }


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.
Posted
Updated 26-Feb-14 22:30pm
v2
Comments
ZurdoDev 26-Feb-14 7:54am    
Failed attempts should be kept track of in the database, in my opinion.

Where exactly are you stuck?
miss786 26-Feb-14 9:04am    
Thank you for your response back. I am not sure, how to go about constructing a code to throw custom errors, if the username does not match or if the username is null. Is their some specific library, i should look into for creating custom error handlers? Thank you for your 'failAttempt' variable feedback, i shall also look into that. Any help much appreciated.
ZurdoDev 26-Feb-14 9:30am    
I don't understand why you want custom errors. However, an easy way is to do

throw "This is a custom error."
miss786 27-Feb-14 4:32am    
Thank you for your reply. Apology for not making my question clearer, but I would like to show exception, when the user fails to login on the client-side. I updated my code but I still cannot see any error message, when i leave the user credentials blank or input incorrect values. Thank you for your help.

1 solution

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900