加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – IdentityServer MVC令牌过期

发布时间:2020-12-16 09:20:34 所属栏目:asp.Net 来源:网络整理
导读:我是Identity Server的新手,我的理解中缺少一个关键概念. 我正在使用 MVC tutorial的代码. 如果我使用属性[Authorize]装饰我的Home控制器并访问我的网站,我会重定向到IdentityServer.然后我使用我的用户名和密码登录.然后我使用一些自定义代码并进行身份验证
我是Identity Server的新手,我的理解中缺少一个关键概念.
我正在使用 MVC tutorial的代码.

如果我使用属性[Authorize]装饰我的Home控制器并访问我的网站,我会重定向到IdentityServer.然后我使用我的用户名和密码登录.然后我使用一些自定义代码并进行身份验证.我得到一个AccessToken,然后我可以访问Home控制器.

我的客户端设置如下:

new Client {
                ClientId = "mvc",ClientName = "MVC Client",AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,ClientSecrets = new List<Secret>{new Secret("secret".Sha256())},RequireConsent = false,AccessTokenLifetime = 1,// where to redirect to after login
                RedirectUris = new List<string>{"http://localhost:5002/signin-oidc"},// where to redirect to after logout
                PostLogoutRedirectUris = new List<string>{"http://localhost:5002"},AllowedScopes = new List<string>
                {
                    StandardScopes.OpenId.Name,StandardScopes.Profile.Name,StandardScopes.OfflineAccess.Name,}
            }

我的访问令牌是

{
  "nbf": 1474697839,"exp": 1474697840,"iss": "http://localhost:5000","aud": "http://localhost:5000/resources","client_id": "mvc","scope": [
    "openid","profile"
  ],"sub": "26296","auth_time": 1474697838,"idp": "local","amr": [
    "pwd"
  ]
}

当我将AccessTokenLifetime设置为1时,我的令牌在被发送到调用API等时将被无效.但我仍然可以访问该网站.

让MVC网站确认我的令牌没有过期的最佳方法是什么?这可能是刷新令牌进入的地方.

注意
AccessTokenLifetime设置为1仅用于测试,因此我可以快速测试.

解决方法

也许是这样的?

var user = HttpContext.Current.User.Identity;
                    if (!user.IsAuthenticated)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读