2016-12-08 1 views
0

Ich möchte 500 Statuscode für try Catch-Blöcke zurückgeben.
aber immer 400 Statuscodes zurückgeben.
Wenn E-Mail und Passwort falsch sind, möchte ich 400 Statuscode und wenn Fehler 500 Statuscode zeigen.ASP.NET WebApi Anmeldung durch Token, wie man Rückkehrstatuscodes unterscheidet?

hier meine Codes. Bitte hilf mir.

public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
     return Task.Factory.StartNew(() => 
     { 

      try 
      { 

       context.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "http://localhost:36725" }); 

       string usertype = context.OwinContext.Get<string>("usertype"); 


       if (usertype == "Profile") 
       { 
        var username = context.UserName; 
        var password = context.Password; 

        var profiles = new Profiles(); 

        Profile profile = profiles.Login(username, password); 

        if (profile != null) 
        { 

         var claims = new List<Claim>() 
          { 
          new Claim("ID", profile.ID.ToString()), 
          new Claim(ClaimTypes.Name, profile.Name), 
          new Claim(ClaimTypes.Surname, profile.Surname), 
          new Claim("ProfilePhotoUrl", profile.ProfilePhotoUrl), 
          new Claim("UserName", profile.UserName), 
          new Claim(ClaimTypes.Role, profile.UserType.Name), 
          new Claim("Language", profile.Language.Name) 
          }; 

         ClaimsIdentity oAutIdentity = new ClaimsIdentity(claims, Startup.OAuthOptions.AuthenticationType); 

         context.Validated(new AuthenticationTicket(oAutIdentity, new AuthenticationProperties() { })); 

        } 
        else 
        { 
         context.SetError("invalid_grant", "The e-mail or password is incorrect"); 
        } 
       } 
       else if (usertype == "Page") 
       { 
        var username = context.UserName; 
        var password = context.Password; 

        var pages = new Pages(); 

        Page page = pages.Login(username, password); 

        if (page != null) 
        { 

         var claims = new List<Claim>() 
          { 
           new Claim("ID", page .ID.ToString()), 
           new Claim(ClaimTypes.Name, page.Name), 
           new Claim("ProfilePhotoUrl", page.ProfilePhotoUrl), 
           new Claim("UserName", page.UserName), 
           new Claim(ClaimTypes.Role, page.UserType.Name) 
          }; 

         ClaimsIdentity oAutIdentity = new ClaimsIdentity(claims, Startup.OAuthOptions.AuthenticationType); 

         context.Validated(new AuthenticationTicket(oAutIdentity, new AuthenticationProperties() { })); 

        } 
        else 
        { 
         context.SetError("invalid_grant", "The e-mail or password is incorrect"); 

        } 
       } 
       else if (usertype == "Anonymous") 
       { 

        var username = context.UserName; 

        var password = context.Password; 

        string name = context.OwinContext.Get<string>("name"); 

        string surname = context.OwinContext.Get<string>("surname"); 


        var profiles = new Profiles(); 

        Profile profile = profiles.Login(name, surname, username, password); 

        if (profile != null) 
        { 

         var claims = new List<Claim>() 
          { 
          new Claim("ID", profile.ID.ToString()), 
          new Claim(ClaimTypes.Name, profile.Name), 
          new Claim(ClaimTypes.Surname, profile.Surname), 
          new Claim(ClaimTypes.Email, profile.Email), 
          new Claim(ClaimTypes.Role, profile.UserType.Name), 
          }; 

         ClaimsIdentity oAutIdentity = new ClaimsIdentity(claims, Startup.OAuthOptions.AuthenticationType); 

         context.Validated(new AuthenticationTicket(oAutIdentity, new AuthenticationProperties() { })); 

        } 
        else 
        { 
         Http.Log log = new Http.Log("An unknown error occurred"); 
         context.SetError("invalid_grant", "An unknown error occurred"); 
        } 
       } 
       else 
       { 

        Http.Log log = new Http.Log("User Type is incorrect"); 
        context.SetError("invalid_grant", "User Type is incorrect"); 
       } 

      } 
      catch (Exception ex) 
      { 

       Http.Log log = new Http.Log(ex.Message + " " + "An unknown error occurred"); 
       context.SetError("invalid_grant", "An unknown error occurred"); 

      } 
     }); 
    } 

Antwort

0

Dies kann durch das Werfen einer HttpResponseException mit dem entsprechenden HttpStatusCode erreicht werden:

if (EmailAndOrPasswordIsWrong) 
{ 
    throw new HttpResponseException(HttpStatusCode.BadRequest); // 400 
} 

if (SomethingElseGoesWrong) 
{ 
    throw new HttpResponseException(HttpStatusCode.InternalServerError); // 500 
} 
+1

Zusammen mit diesem können Sie 'verwenden ExceptionFilters' auch nicht behandelte Ausnahmen in einem gloabal Rahmen für die Handhabung. siehe [Link] (https://www.asp.net/web-api/overview/error-handling/exception-handling) –