首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.NET核+角2/4认证错误

.NET核+角2/4认证错误
EN

Stack Overflow用户
提问于 2017-07-09 15:06:43
回答 1查看 210关注 0票数 1

我使用本网站对角2/4使用.NET核心API身份验证。

注册工作正常,但我在身份验证(登录)时出现了令牌错误。服务器给出了以下错误:

HTTP500:服务器错误--服务器遇到了一个意外的情况,使得它无法完成请求。

这是我的代码:

代码语言:javascript
复制
[AllowAnonymous]
[HttpPost]
public IActionResult Authenticate([FromBody]ApplicationUserDto applicationUserDto)
{
    var appUser = _appUserService.Authenticate(applicationUserDto.Username, applicationUserDto.Password);
        
    if (appUser == null)
        return Unauthorized();
        

    var tokenHandler = new JwtSecurityTokenHandler();
    var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
        
    var tokenDescriptor = new SecurityTokenDescriptor
    {
        Subject = new ClaimsIdentity(new Claim[]
        {
            new Claim(ClaimTypes.Name, appUser.Id)
        }),
        Expires = DateTime.UtcNow.AddDays(7),
        SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
    };

    var token = tokenHandler.CreateToken(tokenDescriptor); //Here returns the Error
    var tokenString = tokenHandler.WriteToken(token);

    // return basic user info (without password) and token to store client side
    return Ok(new
    {
        Id = appUser.Id,
        Username = appUser.Username,
        FirstName = appUser.FirstName,
        LastName = appUser.LastName,
        Token = tokenString
    });
    }

我不知道有什么问题。我仔细地写了代码(而不是复制粘贴)使用网站。有什么问题吗?

EN

回答 1

Stack Overflow用户

发布于 2017-07-10 08:17:18

我已经找到并解决了错误。

我在项目中实现了应用程序洞察(.NET Core )。我又请求了一次以上的要求从角2项目。然后我去了portal.azure.com,在一个标签中打开了Application。我选择了失败的请求来查看更详细的内容。另一个例外的标题是:

System.ArgumentOutOfRangeException at MyProject.Controllers.ApplicationUsersController.Authenticate

然后我点击它获得更多的细节,在那里我发现了问题:

IDX10603: The algorithm: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256' requires the SecurityKey.KeySize to be greater than '128' bits. KeySize reported: '64'. Parameter name: key.KeySize

密钥保存在appsettings.json文件中。所以我只需要给一个更长的秘密钥匙。

现在它没问题了!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44997839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档