Blazor Server (.NET 7) 如何从 ProtectedLocalStorage 获取数据?

huangapple go评论89阅读模式
英文:

In Blazor Server (.net7) how do you grab data from ProtectedLocalStorage?

问题

我的问题是如何从ProtectedLocalStorage中提取存储的数据。当我尝试这样做时,我一直获得检索到的值为空(没有崩溃或错误消息),并且可以将空字符串与另一个字符串连接并显示它(在login.razor中作为我的测试页面使用)。如何在获取值时避免它为空?我主要关注让用户变量正常工作,如果可以做到一个,那么我可以扩展它到需要的任意数量。非常感谢。

_Imports.razor

@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using MongoDB.Bson;
@using MongoDB.Driver;
@using Project
@using Project.Shared
@using Project.Models
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;

login.razor

@page "/login"
@inject ProtectedLocalStorage storage

<!doctype html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>login</title>
</head>
<body>
    <button @onclick="StoreValueAsync">set session data</button>
    <button @onclick="removeValueAsync">remove session data</button>
    <button @onclick="getValueAsync">get values</button>
    @testString
</body>

@code
{
    private string? testString = "teststring";

    public void StoreValueAsync()
    {
        storage.SetAsync("username", "user");
        storage.SetAsync("password", "pass");
    }

    public void removeValueAsync()
    {
        storage.DeleteAsync("username");
        storage.DeleteAsync("password");
    }

    public void getValueAsync()
    {
        string? user = storage.GetAsync<string>("username").ToString();
        var pass = storage.GetAsync<string>("password");

        if (user != null)
        {
            testString = user + " and user is not null";
        }
        else
        {
            testString = user + "and user is null";
        }
    }
}

编辑:

站点和控制台图像未获取到用户名值,按顺序按下的按钮:set session data, get values
Blazor Server (.NET 7) 如何从 ProtectedLocalStorage 获取数据?

Blazor Server (.NET 7) 如何从 ProtectedLocalStorage 获取数据?

英文:

My question is how do I grab stored data from the ProtectedLocalStorage. When I attempted to do so I keep getting blank values for the retrieved value (no crashes or error messages) and am able to concatenate the blank string with another string and display it (using login.razor as my test page). How do I grab the value without it being blank? I am mainly focused on getting the user variable to work as if one can be done then I can expand it to however many are needed. Thank you in advance.

_Imports.razor

@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using MongoDB.Bson;
@using MongoDB.Driver;
@using Project
@using Project.Shared
@using Project.Models
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;

login.razor

@page &quot;/login&quot;
@inject ProtectedLocalStorage storage


&lt;!doctype html&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot; /&gt;
    &lt;title&gt;login&lt;/title&gt;

    
&lt;/head&gt;
  &lt;body&gt;
      &lt;button @onclick = &quot;@StoreValueAsync&quot;&gt;set session data&lt;/button&gt;

      &lt;button @onclick = &quot;@removeValueAsync&quot;&gt;remove session data&lt;/button&gt;

      &lt;button @onclick = &quot;@getValueAsync&quot;&gt;get values&lt;/button&gt;

      @testString

      
  &lt;/body&gt;

@code
{
    private string? testString = &quot;teststring&quot;;

    public void StoreValueAsync()
    {
        storage.SetAsync(&quot;username&quot;, &quot;user&quot;);
        storage.SetAsync(&quot;password&quot;, &quot;pass&quot;);
    }

    public void removeValueAsync()
    {
        storage.DeleteAsync(&quot;username&quot;);
        storage.DeleteAsync(&quot;password&quot;);
    }

    public void getValueAsync()
    {
        string? user = storage.GetAsync&lt;string&gt;(&quot;username&quot;).ToString();
        var pass = storage.GetAsync&lt;string&gt;(&quot;password&quot;);

        if (user != null)
        {
            testString = user + &quot; and user is not null&quot;;
        }
        else
        {
            testString = user + &quot;and user is null&quot;;
        }

    }


}

edit:

images of the site and console not grabbing the username value, the buttons pressed in order prior to screenshots: set session data, get values

Blazor Server (.NET 7) 如何从 ProtectedLocalStorage 获取数据?

Blazor Server (.NET 7) 如何从 ProtectedLocalStorage 获取数据?

答案1

得分: 1

以下是您要翻译的内容:

Updated answer

这是稍微修改过的页面版本。如果这是一个 Blazor 页面,那么所有的头部内容是什么呢?

我认为它的工作方式如下:

@page "/"
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
@inject ProtectedLocalStorage storage

<div>
    <button @onclick="@StoreValueAsync">设置会话数据</button>

    <button @onclick="@removeValueAsync">移除会话数据</button>

    <button @onclick="@getValueAsync">获取值</button>

</div>

<div class="alert alert-info m-3">
    @testString
</div>

@code
{
    private string? testString = "teststring";

    public void StoreValueAsync()
    {
        storage.SetAsync("username", "fred");
        storage.SetAsync("password", "pass");
    }

    public void removeValueAsync()
    {
        storage.DeleteAsync("username");
        storage.DeleteAsync("password");
    }

    public async Task getValueAsync()
    {
        string? user = null;
        var result = await storage.GetAsync<string>("username");

        if (result.Success)
            user = result.Value;

        if (user != null)
        {
            testString = user + " 和用户不为空";
        }
        else
        {
            testString = user + " 和用户为空";
        }
    }
}

这里还有另一个问题和答案,我在几分钟前回答了如何在 Blazor Server 和 WASM 中进行本地存储而无需使用任何库的问题:https://stackoverflow.com/questions/75634022/how-to-grab-data-from-protected-local-storage-blazor-server

这是一个屏幕截图:

Blazor Server (.NET 7) 如何从 ProtectedLocalStorage 获取数据?

英文:

> Updated answer

Here's my slightly modified version of your page. If this is a Blazor Page then what is all the header stuff?

It works as I believe it's supposed to:

@page &quot;/&quot;
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
@inject ProtectedLocalStorage storage


&lt;div&gt;
    &lt;button @onclick=&quot;@StoreValueAsync&quot;&gt;set session data&lt;/button&gt;

    &lt;button @onclick=&quot;@removeValueAsync&quot;&gt;remove session data&lt;/button&gt;

    &lt;button @onclick=&quot;@getValueAsync&quot;&gt;get values&lt;/button&gt;

&lt;/div&gt;

&lt;div class=&quot;alert alert-info m-3&quot;&gt;
    @testString
&lt;/div&gt;


@code
{
    private string? testString = &quot;teststring&quot;;

    public void StoreValueAsync()
    {
        storage.SetAsync(&quot;username&quot;, &quot;fred&quot;);
        storage.SetAsync(&quot;password&quot;, &quot;pass&quot;);
    }

    public void removeValueAsync()
    {
        storage.DeleteAsync(&quot;username&quot;);
        storage.DeleteAsync(&quot;password&quot;);
    }

    public async Task getValueAsync()
    {
        string? user = null;
        var result = await storage.GetAsync&lt;string&gt;(&quot;username&quot;);

        if (result.Success)
            user = result.Value;

        if (user != null)
        {
            testString = user + &quot; and user is not null&quot;;
        }
        else
        {
            testString = user + &quot;and user is null&quot;;
        }
    }
}

There's also another question/answer here I answered a few minutes ago showing how to do local storage without any libraries in both Server and WASM. - https://stackoverflow.com/questions/75634022/how-to-grab-data-from-protected-local-storage-blazor-server

Here's a screenshot:

Blazor Server (.NET 7) 如何从 ProtectedLocalStorage 获取数据?

huangapple
  • 本文由 发表于 2023年3月4日 05:31:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定