简体   繁体   English

在 ASP.NET 应用程序中将身份验证令牌存储到另一个系统的位置

[英]Where to store auth token to another system in ASP.NET application

I have an ASP.NET Web Application that serves documents to the client.我有一个向客户端提供文档的 ASP.NET Web 应用程序。 This document can be in a "separate" system.该文档可以在一个“单独的”系统中。 To get the document from that system I need to login, then it returns a token that I then use on every request I make to it.要从该系统获取文档,我需要登录,然后它返回一个令牌,然后我在向它发出的每个请求中使用该令牌。 (the credentials for that system does not depend on the current user of my Application, I just have fixed username and password) (该系统的凭据不依赖于我的应用程序的当前用户,我只有固定的用户名和密码)

I would like to avoid logging in every time a document is requested, instead I would prefer to store the token (it expires after an hour) internally.我想避免每次请求文档时都登录,而是更愿意在内部存储令牌(它在一个小时后过期)。

What is the recommended way to store this kind of variable?存储这种变量的推荐方法是什么?

Usage of static variable is discouraged and using Session would store the ticket only for a User.不鼓励使用静态变量,使用Session只会为用户存储票证。

I think what you have is app-app authentication, not user-app authentication.我认为您拥有的是应用程序身份验证,而不是用户应用程序身份验证。 In that case, how about saving token in cache with 1 hr expiry?在这种情况下,如何将令牌保存在 1 小时到期的缓存中? So whenever the system tries to read the token, try getting value from cache first.因此,每当系统尝试读取令牌时,请先尝试从缓存中获取值。 If its empty - which means its first time login or expired cache - hit the auth service, get the token and save it to cache.如果它是空的——这意味着它是第一次登录或过期的缓存——命中认证服务,获取令牌并将其保存到缓存中。

May be while in a load balanced environment, you might end up with each server having different tokens which I think is fine in your case.可能在负载平衡的环境中,您最终可能会发现每台服务器都有不同的令牌,我认为这对您来说很好。 Or else you might need to consider something like Redis cache.否则,您可能需要考虑诸如 Redis 缓存之类的东西。

As Josh recommended in comment above, storing token in the database would be better option.正如 Josh 在上面的评论中建议的那样,将令牌存储在数据库中将是更好的选择。

How you refresh that token is a choice you need to make.如何刷新该令牌是您需要做出的选择。

You can request a new token just before 5 minutes the old token expires by running a service or job.您可以在旧令牌到期 5 分钟前通过运行服务或作业请求新令牌。 And your web application will just use the new token to retrieve the documents.您的 Web 应用程序将仅使用新令牌来检索文档。

Also your web application can refresh the token.此外,您的 Web 应用程序可以刷新令牌。 Whenever it uses the token from database, it can check for the expiry and if token is expiring in next 5 minutes the application can renew/refresh the token and store the new token with new expiry in the database and use it to retrieve the documents.每当它使用数据库中的令牌时,它可以检查是否过期,如果令牌在接下来的 5 分钟内过期,应用程序可以更新/刷新令牌并将具有新过期时间的新令牌存储在数据库中,并使用它来检索文档。

If storing in database is too complex then you can consider storing token and expiry on a temporary file on the web server itself and use any of the above two approach to refresh the token.如果存储在数据库中太复杂,那么您可以考虑将令牌和到期时间存储在 Web 服务器本身的临时文件中,并使用上述两种方法中的任何一种来刷新令牌。 The difference will be, the token is stored in a file instead of database.不同之处在于,令牌存储在文件中而不是数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM