|
1 | 1 | using System; |
2 | 2 | using System.Linq; |
3 | 3 | using System.Text; |
| 4 | + |
| 5 | +using Microsoft.AspNetCore.Authentication; |
4 | 6 | using Microsoft.AspNetCore.Http; |
5 | 7 | using Microsoft.AspNetCore.Mvc; |
6 | 8 |
|
@@ -36,8 +38,8 @@ internal static Microsoft.AspNetCore.Http.IResult ToMinimalApiResult(this IResul |
36 | 38 | ResultStatus.Ok => result is Result ? Results.Ok() : Results.Ok(result.GetValue()), |
37 | 39 | ResultStatus.Created => Results.Created("", result.GetValue()), |
38 | 40 | ResultStatus.NotFound => NotFoundEntity(result), |
39 | | - ResultStatus.Unauthorized => Results.Unauthorized(), |
40 | | - ResultStatus.Forbidden => Results.Forbid(), |
| 41 | + ResultStatus.Unauthorized => UnAuthorized(result), |
| 42 | + ResultStatus.Forbidden => Forbidden(result), |
41 | 43 | ResultStatus.Invalid => Results.BadRequest(result.ValidationErrors), |
42 | 44 | ResultStatus.Error => UnprocessableEntity(result), |
43 | 45 | ResultStatus.Conflict => ConflictEntity(result), |
@@ -140,5 +142,47 @@ private static Microsoft.AspNetCore.Http.IResult UnavailableEntity(IResult resul |
140 | 142 | return Results.StatusCode(StatusCodes.Status503ServiceUnavailable); |
141 | 143 | } |
142 | 144 | } |
| 145 | + |
| 146 | + private static Microsoft.AspNetCore.Http.IResult Forbidden(IResult result) |
| 147 | + { |
| 148 | + var details = new StringBuilder("Next error(s) occurred:"); |
| 149 | + |
| 150 | + if (result.Errors.Any()) |
| 151 | + { |
| 152 | + foreach (var error in result.Errors) details.Append("* ").Append(error).AppendLine(); |
| 153 | + |
| 154 | + return Results.Problem(new ProblemDetails |
| 155 | + { |
| 156 | + Title = "Forbidden.", |
| 157 | + Detail = details.ToString(), |
| 158 | + Status = StatusCodes.Status403Forbidden |
| 159 | + }); |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + return Results.Forbid(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private static Microsoft.AspNetCore.Http.IResult UnAuthorized(IResult result) |
| 168 | + { |
| 169 | + var details = new StringBuilder("Next error(s) occurred:"); |
| 170 | + |
| 171 | + if (result.Errors.Any()) |
| 172 | + { |
| 173 | + foreach (var error in result.Errors) details.Append("* ").Append(error).AppendLine(); |
| 174 | + |
| 175 | + return Results.Problem(new ProblemDetails |
| 176 | + { |
| 177 | + Title = "Unauthorized.", |
| 178 | + Detail = details.ToString(), |
| 179 | + Status = StatusCodes.Status401Unauthorized |
| 180 | + }); |
| 181 | + } |
| 182 | + else |
| 183 | + { |
| 184 | + return Results.Unauthorized(); |
| 185 | + } |
| 186 | + } |
143 | 187 | } |
144 | 188 | #endif |
0 commit comments