mohsen
mohsen

Reputation: 1806

TokenCreationRequest does not contain a definition for Resources

I had a question for creating token without username and password here how can I get token from token endpoint without username and password in identityserver4?

But recently I decided to update my projects to use .net5 ,but after that I found that my host don't support .net5 so I removed All .net5 and Installed the versions to 3.1.But below error is there I don't know how solve it

Error   CS1061  'TokenCreationRequest' does not contain a definition for 'Resources' and no accessible extension method 'Resources' accepting a first argument of type 'TokenCreationRequest' could be found (are you missing a using directive or an assembly reference?)

Error   CS7036  There is no argument given that corresponds to the required formal parameter 'apiScopes' of 'Resources.Resources(IEnumerable<IdentityResource>, IEnumerable<ApiResource>, IEnumerable<ApiScope>)'

My code:

[HttpPost("Activate")]
        [AllowAnonymous]
        public async Task<IActionResult> Activate([FromBody] SignUpPhoneModel model, [FromServices] ITokenService TS, [FromServices] IUserClaimsPrincipalFactory<JooyaIdentityUser> principalFactory, [FromServices] IdentityServerOptions options)
        {           
            JooyaIdentityUser CurrentUser = await db.Users.Where(e => e.PhoneNumber == model.PhoneNumber).FirstOrDefaultAsync();
            
            if (!await UserManager.VerifyChangePhoneNumberTokenAsync(CurrentUser, model.ActivationCode, model.PhoneNumber))
            {
                return BadRequest("Activation Code in not correct");
            }
            CurrentUser.PhoneNumberConfirmed = true;
            await db.SaveChangesAsync();
            await UserManager.UpdateSecurityStampAsync(CurrentUser);
            var Request = new TokenCreationRequest();
            var IdentityPricipal = await principalFactory.CreateAsync(CurrentUser);
            var IdentityUser = new IdentityServerUser(CurrentUser.Id.ToString());
            IdentityUser.AdditionalClaims = IdentityPricipal.Claims.ToArray();
            IdentityUser.DisplayName = CurrentUser.UserName;
            IdentityUser.AuthenticationTime = System.DateTime.UtcNow;
            IdentityUser.IdentityProvider = IdentityServerConstants.LocalIdentityProvider;
            Request.Subject = IdentityUser.CreatePrincipal();
            Request.IncludeAllIdentityClaims = true;
            Request.ValidatedRequest = new ValidatedRequest();
            Request.ValidatedRequest.Subject = Request.Subject;

            Request.ValidatedRequest.SetClient(SeedConfig.GetClients().Where(e => e.ClientId == model.ClientId).First());
            List<ApiResource> Apis = new List<ApiResource>();
            Apis.Add(SeedConfig.GetApis().Where(e => e.Name == "IdentityServerApi").First());
            Apis.Add(SeedConfig.GetApis().Where(e => e.Name == model.ApiName).First());
            Request.Resources = new Resources(SeedConfig.GetIdentityResources(), Apis);// <=== Error is here
            Request.ValidatedRequest.Options = options;
            Request.ValidatedRequest.ClientClaims = IdentityUser.AdditionalClaims;
            var Token = await TS.CreateAccessTokenAsync(Request);
            Token.Issuer = HttpContext.Request.Scheme + "://" + HttpContext.Request.Host.Value;
            Token.Lifetime = 32000000;
            var TokenValue = await TS.CreateSecurityTokenAsync(Token);
            TokenModel tm = new TokenModel()
            {
                access_token = TokenValue
            };
            return Ok(tm);
        }

Upvotes: 0

Views: 809

Answers (1)

Alpha75
Alpha75

Reputation: 2280

You are using a versión of IdentityServer4 that is not compatible with your code. The model of TokenCreationRequest has changed. Last version of IdentityServer4 (4.1.1) hasn't the Resources property. It uses ValidatedResources of type ResourceValidationResult instead.

You need to adjust your code or downgrade to an older version of IdentityServer < 4.0.0.

Upvotes: 1

Related Questions