Skip to content

Minimal API MapGet(...).Produces( and .Produces<T>( not available?Β #35835

@DevTKSS

Description

@DevTKSS

Description

Somehow I am not getting the Produces Methods on the OpenApi Endpoint for MapGet, but I dont know why πŸ€” maybe someone can tell me, whats wrong?

Image

if I would rely on Task<IResult> for my Method signature or even with that above shown signature, I am getting that unmentioned in the docs "Return value would be ignored":

Image

which would be quite bad, because I would like to somehow show my user that we got him Signed In successfully πŸ˜… but I would not want to have to use the Identity Platform things itself just to do that with minimal api, should not be required, isnt it?

tryed to add any known to me namespaces that could be missing, while I do also have global usings in place, but wanted to make sure that I do not miss one:

Image

not even Copilot is able to tell me whats up with that strange return value getting thrown away thing...

As this is just code following along a sample I found for WinUI oAuth as server side, here is the used code for this:

using System.Net.Mime;
using System.Text;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Authentication;

namespace DevTKSS.MyManufacturerERP.WebApi.Apis;

 public static RouteGroupBuilder MapAuthenticationEndpoints(this IEndpointRouteBuilder routes)
 {
     // Map the authentication endpoints
     var connectgroup = routes.MapGroup("/connect")     
                           .WithTags("Authentication");

     connectgroup.MapGet("/authorize",Authorize)
         .AllowAnonymous();


     return connectgroup;
 }

    private static async Task<Results<ContentHttpResult,BadRequest>> Authorize(HttpContext context)
    {
        var request = context.GetOpenIddictServerRequest();
        if (request is null)
            return TypedResults.BadRequest();

        var identity = new ClaimsIdentity(OpenIddictServerAspNetCoreDefaults.AuthenticationScheme, Claims.Name, Claims.Role);
        identity.AddClaim(Claims.Subject, "dummy_user_id");
        identity.AddClaim(Claims.Name, "Test User");

        var principal = new ClaimsPrincipal(identity);
        principal.SetScopes(Scopes.OpenId, Scopes.Profile, Scopes.Email);

        await context.SignInAsync(OpenIddictServerAspNetCoreDefaults.AuthenticationScheme, principal);

        context.Response.Clear();

        var template = await File.ReadAllTextAsync("page.html");
        var code = Uri.EscapeDataString(context.GetOpenIddictServerResponse()!.Code!);
        var state = Uri.EscapeDataString(context.GetOpenIddictServerResponse()!.State!);
        var redirect = new UriBuilder(request.RedirectUri!)
        {
            Query = $"code={code}&state={state}"
        }.Uri.ToString();


        var html = string.Format(template, redirect);

        context.Response.ContentType = "text/html; charset=utf-8";
        return TypedResults.Content(html,MediaTypeNames.Text.Html,Encoding.UTF8,StatusCodes.Status200OK);

    }
}

the used html template where the string.Format should do that replace:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Authentication OK</title>
    <script>
      window.location.href = "{0}";
    </script>
    <style>
      body {{ font-family: sans-serif; text-align: center; margin-top: 50px; }}
    </style>
</head>
<body>
  <h1>Login completed!</h1>
  <p>If you do not see the app open automatically, click here:</p>
  <a href="{0}">Open the desktop app</a>
  <p>You can now close this window.</p>
</body>
</html>

this is my global usings content:

Image

originally this was not having any return and directly in the Programm.cs πŸ€”
https://github.com/IlGalvo/WinUI3-OAuth2Manager-Sample/blob/a7a2cbc262665a99c07fcc51c9f42a4616f18eb2/Server/Server/Program.cs#L64-L91

I tryed to seperate it to own file and use the RouteGroupBuilder, but to build my response that would need the httpcontext I think. And reading the ms docs on the linked page below, we should prefer returning a BadRequest instead of throwing exceptions, but doing this in a non task seperated MapGet( like in that sample, this is giving me a red underline trying to make it return this.

Can anyone tell why I am not getting that Produces and advise how to set this up withouth this hell of added overhead the linked docs page does introduce us to do, just for showing the user this cute little html page?

Page URL

https://learn.microsoft.com/de-de/aspnet/core/fundamentals/minimal-apis/responses?view=aspnetcore-9.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/minimal-apis/responses.md

Document ID

f0e5da68-cd54-ce74-1ff3-ebd7d9fd09a4

Platform Id

03e8dae5-648b-cf4d-d50f-b00f0d8fc967

Article author

@brunolins16

Metadata

  • ID: f0e5da68-cd54-ce74-1ff3-ebd7d9fd09a4
  • PlatformId: 03e8dae5-648b-cf4d-d50f-b00f0d8fc967
  • Service: aspnet-core
  • Sub-service: fundamentals

Related Issues

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions