简体   繁体   English

如何跨页面处理自定义令牌

[英]How to handle custom token across pages

I have a custom log in that returns an encrypted Token, which indicates that a user is logged in. This Token is passed to another page(Dash.aspx) via QueryString. 我有一个自定义日志,该日志返回一个加密的令牌,该令牌指示用户已登录。此令牌通过QueryString传递到另一个页面(Dash.aspx)。

Dash.aspx takes the token from the QueryString and posts it to a hidden field on the page. Dash.aspx从QueryString中获取令牌,并将其发布到页面上的隐藏字段中。 Javascript reads that value and holds it in memory. Javascript读取该值并将其保存在内存中。 That Token is then used to make web service calls. 该令牌然后用于进行Web服务调用。 When these calls complete a new Token value is returned, and javascript stores that value (replacing the old one). 当这些调用完成时,将返回一个新的Token值,并且javascript存储该值(替换旧的Token值)。

I want to add new pages for access after log in. These pages will need a valid Token passed to them. 我想在登录后添加新页面以进行访问。这些页面将需要一个有效的令牌传递给它们。 A user would move from Dash.aspx to one of these new pages and back (so just a few different links at the top of a Masterpage) 用户将从Dash.aspx移至这些新页面之一,然后又返回(因此,在母版页顶部仅需几个不同的链接)

I don't like passing the Token via QueryString. 我不喜欢通过QueryString传递令牌。 And I am not sure how to keep the Token updated where accessible on page change. 而且我不确定如何在页面更改可访问的地方保持令牌的更新。 I want to avoid using Session to store and pass the Token if possible 我想避免使用Session来存储和传递令牌(如果可能)

How can I pass my Token more discreetly and make sure it always passes the most up to date value? 如何更谨慎地传递令牌并确保其始终传递最新值?

I realize this is a fairly broad question, but im at a loss. 我意识到这是一个相当广泛的问题,但我无所适从。 I feel like there is probably some pre built idea that will handle this, i just dont know what or how to use it. 我觉得可能有一些预构建的想法可以解决这个问题,我只是不知道该怎么用或如何使用它。

Thanks 谢谢

Update 更新资料

So an example was asked for: 因此,要求提供一个示例:

Step 1: User logs in - > zholen/zholen123 步骤1:用户登录-> zholen / zholen123

  • Service is called to validate username and password -> returns Token ('ABC') 调用服务以验证用户名和密码->返回令牌('ABC')
  • Redirect to Page Dash.aspx?token=ABC 重定向到Page Dash.aspx?token = ABC

Step 2: Dash.aspx grabs token from querystring and assigns to hidden field on page 步骤2:Dash.aspx从querystring中获取令牌并分配给页面上的隐藏字段

  • Javascript object grabs token from hidden field and stores internally Javascript对象从隐藏字段中获取令牌并在内部存储
  • JS Object makes several async calls to various services, each service returns a new updated Token, internal token is updated with new value( Tokens expire every 30 min ) JS Object对各种服务进行了几次异步调用,每个服务都返回一个新的更新令牌,内部令牌被更新为新值( 令牌每30分钟过期一次

Desired new steps 所需的新步骤

Step 3: Move from Dash.aspx to Account.aspx 步骤3:从Dash.aspx移至Account.aspx

  • Account.aspx requires valid Token to load Account.aspx需要有效令牌才能加载
  • Call more services and change Token 致电更多服务并更改令牌

Step 4: Move from Account to Dash.aspx with up to date token 步骤4:使用最新令牌从Account移到Dash.aspx


Service calls are made either via a Web Service(asmx) or through page methods depending whether the action desires a data return (asmx) or an html return(page method -> table prefilled with data) or on page load 服务调用是通过Web Service(asmx)还是通过页面方法进行的,具体取决于操作是需要数据返回(asmx)还是html return(页面方法->预先填充数据的表格) 或页面加载

Based on suggestion of Cookies, I think it would be possible to reset the cookie with the new token value during these calls on C# end, assuming that i could do that kind of thing from an ASMX and that the async of the whole thing wouldn't cause issues. 基于Cookie的建议,我认为在C#端进行这些调用期间,可以使用新的令牌值重置Cookie,前提是我可以通过ASMX进行此类操作,并且可以使整个操作保持异步状态。导致问题。

Also I can make the JS object which internally stores an up to date token place that value back into the hidden field if that would help make it accessible from the C# end. 另外,如果可以从C#端访问它,我还可以使JS对象在内部存储该值的最新令牌位置,并将该值放回到隐藏字段中。

You could use cookies... You may want to use your intermediate encryption as cookies can be read externally. 您可以使用cookie ...您可能希望使用中间加密,因为可以从外部读取cookie。

        //c#

        HttpCookie cookie = new HttpCookie("myTokenCookie");
        cookie.Value = tokenString;
        Response.SetCookie(cookie);

        // then get it back later
        s = Request.Cookies["myTokenCookie"].Value;

        // then you could write it into a hidden input for retrieval in JS

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

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