Comment renvoyer le 401 avec un message personnalisé ?
P粉851401475
P粉851401475 2024-01-01 15:06:48
0
1
445

Dans mon API Web, j'attrape l'AuthenticationException lancée par la méthode DoCdssLoginTests(...) auquel cas je renvoie une erreur 401, mais je souhaite pouvoir l'utiliser pour envoyer un message personnalisé. ≪ /p>

Le membre de contenu n'est pas disponible et je ne peux pas remplacer la réponse par un nouveau membre.

J'ai trouvé ce fil mais il ne fait vraiment rien dans ma méthode puisque je renvoie une chaîne au lieu d'un IHttpActionResult.

Je peux cibler les exceptions .NET levées pour "Non autorisé" ou "Non validé"

[ HttpGet ]
[ Route( "/DoCdssLoginTests" ) ]
public string DoCdssLoginTests( string sAdName )
{
    log.LogTrace( "AdStudentController::DoCdssLoginTests() - in" );
   
    try
    { 
        return studBll.DoCdssLoginTests( sAdName );
    }
    catch ( AuthenticationException ex )
    {
        Response.StatusCode = ( int ) HttpStatusCode.Unauthorized;
        //<--- I WANT TO SET A CUSTOM MESSAGE HERE TO BE RETURNED TO VUE CLIENT. - EWB
        return "N";
    }
    catch ( Exception ex )
    {
        throw;
    }

    return "OK";
}


P粉851401475
P粉851401475

répondre à tous(1)
P粉567281015

ActionResult doit être renvoyé avant que Unauthorized() puisse être renvoyé comme ceci :

 ;
public ActionResult<string> DoCdssLoginTests(string sAdName)
{
    log.LogTrace("AdStudentController::DoCdssLoginTests() - in");
    try
    {
        return studBll.DoCdssLoginTests(sAdName);
    }
    catch (AuthenticationException ex)
    {
        return Unauthorized("Your custom message");
    }
    catch (Exception ex)
    {
        throw;
    }
    return "OK";
}
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!