'How to store an unique Blazor sessionID for each client?
We are using a shared Razor Class Library for our Blazor applications containing all components, that both our Blazor Server and Blazor WebAssembly startup projects are using.
However, when we open another tab or browser window, going to the same URL, we noticed that the session persists.
We tried creating an unique guid in JavaScript, and invoking it to the app using IJSRuntime. That unique guid is then used inside all components to show the correct data loaded from our API. It doesn't always work however, and it is not good perfomance wise either.
Because we are using a shared Razor Class Library, we are not able to use the ProtectedSessionStorage. How can we get unique sessions without using JavaScript interop calls?
Solution 1:[1]
To give each Window/BrowserTab-Session a unique-Id just add a scoped service in Program.cs. If this is what your want.
Each instance of the App gets a unique Id that can be passed on to your library-components, in order to identify a tab/window instance as long as this session lastes.
Code for Blazor Webassably
Add in Program.cs
builder.Services.AddScoped<MyIdService>();
Add the MyIdService.cs class
public class MyIdService
{
public Guid MyGuid { get; set; } = Guid.NewGuid();
}
Add/inject this to every page you need this behavior. Or to make it more simple just add it to the shared/mainlayout.razor file.
@inherits LayoutComponentBase
@inject MyIdService MyIdService
<div class="page">
<div class="sidebar">
<NavMenu />
</div>
<main>
<div class="top-row px-4">
<h4>@MyIdService.MyGuid</h4>
</div>
<article class="content px-4">
@Body
</article>
</main>
</div>
The Id is only good as long as a user session lasts. If you want to store state of every user for later use. You would need a userlogin and some sort of datapersisting.
Solution 2:[2]
You may use Python's email library but usually, the examples are not really straightforward and simple especially if you are new. For this reason, I actually made a package that most likely is enough for your needs regarding sending emails.
Just pip install it:
pip install redmail
Then to solve your problem:
from pathlib import Path
from redmail import EmailSender
email = EmailSender(
host="<YOUR SMTP SERVER>",
port=1,
user_name="[email protected]", # If not needed, don't pass this
password="<YOUR PASSWORD>" # If not needed, don't pass this
)
# Check if file exists
if Path("path/to/file.csv").is_file():
# File does exists, send the email
email.send(
receivers=["[email protected]"],
subject="File is now found",
)
If you want to include a message, pass text and/or html parameters and write the email message as a string. You may also set the receivers as a default (attribute of the email instance) if repeatedly send you an email in different sections of your code.
Red Mail is well tested and well documented open-source library with features such as including attachments, embedding images, templating text or HTML and preformatted errors.
Documentation: https://red-mail.readthedocs.io/en/latest/
Source code: https://github.com/Miksus/red-mail
Note that pathlib is from standard library. It is a handy library for manipulating file paths. You could also use os.path.exists if you prefer that.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | miksus |

