0

I have an angular app which send a login request to the server and gets back an error which I throw from the server in case the username or password are invalid or incorrect. Anyway, I have a function on the $http service that gets the error message, but for some reason, the error message comes as a long string of HTML tags and the Exception I throw is somewhere inside that HTML. I want to be able to get the raw message without the customizations or whatever you'd call it, that the server or browser do.

I'll appreciate any help from you.

Update:

My mistake was that I threw Exceptions on my server instead of returning an error. Now I fixed it and i'm returning Errors but I want to return also a status code or a some proper 'http response message'. The method SetError does not allow me this. Is there any other way to do that?

Here is my method:

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
        using (AuthRepository _repo = new AuthRepository())
        {
            bool user = _repo.Authenticate(context.UserName, context.Password);

            if (user == false)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }
        }

        var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        identity.AddClaim(new Claim("sub", context.UserName));
        identity.AddClaim(new Claim("role", "user"));

        context.Validated(identity);
    }
Joe
  • 443
  • 1
  • 4
  • 21

1 Answers1

0

In theory you can use $http like this:

$http.post(url, payload, config)
    .then(function success(response) {
        // do successful stuff here  
    },
    function err(response) {
        // response.data - find error message here
        // response.status - find error code here 
        // treat error here
    });

More info about this here.

However, it sounds to me that you may have a server side issue, regarding the way you respond back to the client. Maybe some code would be useful to troubleshot your problem.

LE: Regarding your status code issue check out this stackoverflow issue, maybe you can figure it out how to apply it on your code base.

From what I can see you should have a Response property on your context and you can set a StatusCode on that. Make sure you read all the answers over there, since I am not a C# developer I just looked it up on google.

Community
  • 1
  • 1
Peter_Fretter
  • 1,135
  • 12
  • 20