微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net – 承载令牌在重新部署后变为无效

我们将ASP.NET MVC 5.x WebAPI 2.x Web应用程序作为Azure云服务运行,并使用令牌授权对我们的服务进行REST API调用.

问题是:每次我们重新部署我们的应用程序时 – 所有当前令牌都变为无效(服务器返回任何请求的“未授权”响应).

问题是:为什么会发生以及如何防止这种行为?

UPD:
以下是发出令牌的代码

public string GetoAuthToken(IUser user) {
    if (user  != null) {
        var identity = new ClaimsIdentity(Startup.OAuthOptions.AuthenticationType);
        identity.AddClaim(new Claim(ClaimTypes.Name,user.UserName));
        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier,user.Id));
        AuthenticationTicket ticket = new AuthenticationTicket(identity,new AuthenticationProperties());
        var currentUtc = DateTime.UtcNow;
        ticket.Properties.IssuedUtc = currentUtc;
        ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromDays(36600)); //About 100 years
        string Accesstoken = Startup.OAuthOptions.AccesstokenFormat.Protect(ticket);
        return Accesstoken;
    }
    return "";
}

UPD2:
看起来认令牌endpoing(/ Token)生成的令牌在重新部署后不会变得无效 – 所以问题(我认为)是我们为“手工”令牌设置的一些属性.
我在哪里可以找到创建认令牌的代码(由/ Token端点返回)?

解决方法

问题解决了.
看来认创建的AccesstokenFormat使用machineKey来生成令牌.显然,这些密钥对于生产和暂存VM来说是不同的.
解决方案相当容易.您需要生成自己的机器密钥并将其添加到项目的Web.Config文件中:

<system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" relaxedUrlToFileSystemMapping="true" />

    <machineKey
      validationKey="YOUR VALIDATION KEY GOES HERE"
      decryptionKey="YOUR DECRYPTION KEY GOES HERE"
      validation="SHA1" decryption="AES"
    />

有关此方法的详细信息,请阅读本文的“步骤5 …”部分:
http://bitoftech.net/2014/09/24/decouple-owin-authorization-server-resource-server-oauth-2-0-web-api/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐