在VB.Net中访问js localStorage存储的值

发布于 2024-09-26 18:23:47 字数 107 浏览 3 评论 0原文

我有一个登录页面,将一些值存储到 localStorage (html5),然后继续到 VB.Net 页面。我正在寻找 VB 中的一种方法,可以读回这些存储的值并使它们成为 VB 变量。有什么想法吗?

I have a login page that stores a few values to localStorage (html5) then continues to a VB.Net page. I am looking for a method in VB that can read back those stored values and make them VB vars. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2024-10-03 18:23:47

VB.NET 代码隐藏在服务器上运行,无法直接访问浏览器的本地存储 API。

不过,您可以使用 JavaScript 轻松填写登录页面上的一些隐藏字段,这些字段将在提交时发布,并且可以从 .NET 页面的代码隐藏中读取。

像这样的东西(未经测试):

this.document.getElementById("HIDDEN_FIELD_ID").value = localStorage.STORED_VALUE;
...
<input type="hidden" id="HIDDEN_FIELD_ID" />
...

在.NET页面上,该值可以这样读取:

Request.Form("HIDDEN_FIELD_ID")

(还有其他方法,但这个很容易掌握。)

请注意,用户可以访问(和修改)localStorage 中的登录数据,因此请确保不会造成安全风险。

The VB.NET code-behind is running on the server and has no direct access to the local storage API of the browser.

You can, however, easily fill some hidden fields on the login page, using JavaScript, which will be posted on submit and can be read from the code-behind of the .NET page.

Something like this (not tested):

this.document.getElementById("HIDDEN_FIELD_ID").value = localStorage.STORED_VALUE;
...
<input type="hidden" id="HIDDEN_FIELD_ID" />
...

On the .NET page the value can be read like:

Request.Form("HIDDEN_FIELD_ID")

(There are other ways, but this one is easy to grasp.)

Be aware that login data in localStorage can be accessed (and modified) by the user, so make sure you are not creating a security risk.

夏末染殇 2024-10-03 18:23:47

此示例将上述概念与 VB 代码结合使用:

这是 html body 元素:

<body>
<form id="form1" runat="server">
<asp:HiddenField ID="hfLoaded" runat="server" />
<asp:HiddenField ID="hfLocalStorage" runat="server" />
</form>
<script type="text/javascript">
    // Load LocalStorage
    localStorage.setItem('strData', 'Local storage string to put into code behind');


    function sendLocalStorageDataToServer()
    {
        // This function puts the localStorage value in the hidden field and submits the form to the server.
        document.getElementById('<%=hfLocalStorage.ClientID%>').value = localStorage.getItem('strData');
        document.getElementById('<%=form1.ClientID%>').submit();
    }

    // This checks to see if the code behind has received the value. If not, calls the function above.
    if (document.getElementById('<%=hfLoaded.ClientID%>').value != 'Loaded')
        sendLocalStorageDataToServer();
</script>

这是页面加载事件:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim s As String
    s = hfLocalStorage.Value

    'This next line prevents the javascript from submitting the form again.
    hfLoaded.Value = "Loaded"
End Sub

现在您的代码背后有可用的 localStorage 值。

This sample uses the above concept with VB Code:

Here is the html body element:

<body>
<form id="form1" runat="server">
<asp:HiddenField ID="hfLoaded" runat="server" />
<asp:HiddenField ID="hfLocalStorage" runat="server" />
</form>
<script type="text/javascript">
    // Load LocalStorage
    localStorage.setItem('strData', 'Local storage string to put into code behind');


    function sendLocalStorageDataToServer()
    {
        // This function puts the localStorage value in the hidden field and submits the form to the server.
        document.getElementById('<%=hfLocalStorage.ClientID%>').value = localStorage.getItem('strData');
        document.getElementById('<%=form1.ClientID%>').submit();
    }

    // This checks to see if the code behind has received the value. If not, calls the function above.
    if (document.getElementById('<%=hfLoaded.ClientID%>').value != 'Loaded')
        sendLocalStorageDataToServer();
</script>

Here is the page load event:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim s As String
    s = hfLocalStorage.Value

    'This next line prevents the javascript from submitting the form again.
    hfLoaded.Value = "Loaded"
End Sub

Now your code behind has the localStorage value available to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文