英文:
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
英文:
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 "/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";
}
}
}
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
答案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
这是一个屏幕截图:
英文:
> 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 "/"
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
@inject ProtectedLocalStorage storage
<div>
<button @onclick="@StoreValueAsync">set session data</button>
<button @onclick="@removeValueAsync">remove session data</button>
<button @onclick="@getValueAsync">get values</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 + " and user is not null";
}
else
{
testString = user + "and user is null";
}
}
}
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论