Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# JWT令牌标识服务器_C#_Asp.net_Identityserver4 - Fatal编程技术网

C# JWT令牌标识服务器

C# JWT令牌标识服务器,c#,asp.net,identityserver4,C#,Asp.net,Identityserver4,有人能帮我将jwt令牌集成到web.api核心中吗。所以我尝试通过IdentityServer 4生成jwt令牌。下面是它的样子: [HttpPost] public async Task<IActionResult> Index(LoginViewModel loginViewModel) { var user = await _userManager.FindByEmailAsync(loginViewModel.Email);

有人能帮我将jwt令牌集成到web.api核心中吗。所以我尝试通过IdentityServer 4生成jwt令牌。下面是它的样子:

        [HttpPost]
    public async Task<IActionResult> Index(LoginViewModel loginViewModel)
    {
        var user = await _userManager.FindByEmailAsync(loginViewModel.Email);

        if(user != null)
        {
            var isCredosValid = await _signInManager.PasswordSignInAsync(loginViewModel.Email, loginViewModel.Password, loginViewModel.RememberMe, false);

            if (isCredosValid.Succeeded)
            {
                var disco = await DiscoveryClient.GetAsync("https://localhost:44351");
                var tokenClient = new TokenClient(disco.TokenEndpoint, _identityServerConfig.ClientId, _identityServerConfig.Secret);

                var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync(
                    loginViewModel.Email, loginViewModel.Password,_identityServerConfig.AllowedScope);

                return Json(tokenResponse.Json);
            }
        }

        return BadRequest();
    }
[HttpPost]
公共异步任务索引(LoginViewModel LoginViewModel)
{
var user=await\u userManager.findbyemailsync(loginViewModel.Email);
如果(用户!=null)
{
var iscredesvalid=wait _signInManager.PasswordSignInAsync(loginViewModel.Email,loginViewModel.Password,loginViewModel.RememberMe,false);
如果(isCredosValid.successed)
{
var disco=await DiscoveryClient.GetAsync(“https://localhost:44351");
var tokenClient=new tokenClient(disco.TokenEndpoint,_identityServerConfig.ClientId,_identityServerConfig.Secret);
var tokenResponse=wait tokenClient.RequestResourceOwnerPasswordAsync(
loginViewModel.Email、loginViewModel.Password、_identityServerConfig.AllowedScope);
返回Json(tokenResponse.Json);
}
}
返回请求();
}
这个方法毫无问题地给了我一个令牌,但是一旦我尝试访问另一个具有角色限制的方法,比如Authorize(Roles=“User”),Api允许我在没有授权的情况下调用这个方法,所以它似乎不起作用。
我是否应该以某种方式验证手动发送到请求头中的令牌以及默认asp.net用户标识是否内置了用于检查令牌的支持?

除了您的问题,用户密码似乎位于两个位置,一个是本地,一个是IdentityServer,原因是什么?为什么不使用OpenIdConnect中间件将用户重定向到identityserver而不是此自制机制?使用像identityserver这样的外部身份提供程序(IdP)的要点是,您的应用程序不必处理用户密码,用户直接使用IdP进行身份验证,您的应用程序信任它从IdP收到的令牌。@WouterHuysentruit获取它。谢谢但当用户尝试使用令牌执行具有角色限制的操作时,验证令牌又如何呢。据我所知,基本asp.net标识不支持令牌的自动解密,我需要定义自定义标识?不确定“验证令牌”的含义,但根据您的应用程序,使用OpenIdConnect中间件,一旦收到来自IdP的令牌,您可以创建自己的JWT令牌或Cookie,其中包含您想要的任何声明。这可以通过处理和转换索赔实体来实现。如果您在JWT中有来自IdP的角色声明,那么这个处理程序也是调试您获得的声明的好地方……鉴于这些信息,我认为实际上可能会解决您的问题。