简体   繁体   English

OpenIdDict 未从.Net Core API 返回令牌

[英]OpenIdDict not returning Token from .Net Core API

I have a.Net Core API project, where I'm using OpenIDDict to authenticate.我有一个.Net Core API 项目,我在其中使用 OpenIDDict 进行身份验证。 I refered there official repository however it is not returning Token to end user (currently i'm testing with postman)我参考了官方存储库,但是它没有将令牌返回给最终用户(目前我正在使用邮递员进行测试)

Here is my Program.cs file这是我的 Program.cs 文件

  //OPENID
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{ options.UseSqlServer(builder.Configuration.GetConnectionString("ApplicationDb"));
    options.UseOpenIddict();
});

// Register the Identity services.
//builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
//    .AddEntityFrameworkStores<ApplicationDbContext>()
//    .AddDefaultTokenProviders();

builder.Services.Configure<IdentityOptions>(options =>
{
    options.ClaimsIdentity.UserNameClaimType = Claims.Name;
    options.ClaimsIdentity.UserIdClaimType = Claims.Subject;
    options.ClaimsIdentity.RoleClaimType = Claims.Role;
    options.ClaimsIdentity.EmailClaimType = Claims.Email;
});

builder.Services.AddQuartz(options =>
{
    options.UseMicrosoftDependencyInjectionJobFactory();
    options.UseSimpleTypeLoader();
    options.UseInMemoryStore();
});

builder.Services.AddOpenIddict()

    // Register the OpenIddict core components.
    .AddCore(options =>
    {
            // Configure OpenIddict to use the Entity Framework Core stores and models.
            // Note: call ReplaceDefaultEntities() to replace the default OpenIddict entities.
        options.UseEntityFrameworkCore()
               .UseDbContext<ApplicationDbContext>();

            // Enable Quartz.NET integration.
        options.UseQuartz();
    })

    // Register the OpenIddict server components.
    .AddServer(options =>
    {
            // Enable the token endpoint.
        options.SetTokenEndpointUris("/connect/token");
        options.AllowPasswordFlow();
        options.AcceptAnonymousClients();
        options.AddDevelopmentEncryptionCertificate()
               .AddDevelopmentSigningCertificate();
        options.UseAspNetCore()
               .EnableTokenEndpointPassthrough();
    })

    // Register the OpenIddict validation components.
    .AddValidation(options =>
    {
        options.UseLocalServer();
        options.UseAspNetCore();
    });...

My AuthorizationController.cs file我的 AuthorizationController.cs 文件

 [HttpPost("~/connect/token"), Produces("application/json")]
public async Task<IActionResult> Exchange()
{
    var request = HttpContext.GetOpenIddictServerRequest();
    var claimsPrincipal = new ClaimsPrincipal();
    if (request.IsPasswordGrantType())
    {
        try
        {
            var user = await _userManager.FindByNameAsync(request.Username);
            if (user == null)
            {
                //Return Error message
            }

            var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, lockoutOnFailure: true);
            if (!result.Succeeded)
            {
                 //Return Error message
            }
            var principal = await _signInManager.CreateUserPrincipalAsync(user);
            principal.SetScopes(new[]
            {
            Scopes.OpenId,
            Scopes.Email,
            Scopes.Profile,
            Scopes.Roles
        }.Intersect(request.GetScopes()));

            foreach (var claim in principal.Claims)
            {
                claim.SetDestinations(GetDestinations(claim, principal));
            }
            return SignIn(principal, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
 ...

It should return a token similar to this answer however I am getting 500 status in postman.它应该返回与此答案类似的令牌,但是我在 postman 中获得了 500 状态。 The error is The entity type 'OpenIddictEntityFrameworkCoreToken' was not found.错误是找不到实体类型“OpenIddictEntityFrameworkCoreToken”。 Ensure that the entity type has been added to the model.确保实体类型已添加到 model。

Not sure, I need to create tables for this?不确定,我需要为此创建表吗? Or I missed something?还是我错过了什么? I can see in official OpenIdDict site , they haven't mentioned anything like that.我可以在OpenIdDict 官方网站上看到,他们没有提到类似的东西。

I'm using.Net 6, VS 2022.我正在使用.Net 6,VS 2022。

Did you try adding?您是否尝试添加?

services.AddIdentity<User, Role>()
    .AddSignInManager()
    .AddUserStore<UserStore>()
    .AddRoleStore<RoleStore>()
    .AddUserManager<UserManager<User>>();

Check this blog检查这个博客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM