Prevent Blazor Mud Snackbar from opening across different browser sessions - c#

I've followed the guide here on creating a MudBlazor Snackbar notification on a Blazor server app:
https://mudblazor.com/components/snackbar
However when I publish my site, and invoke the "ShowSnackbar" method below on a user machine, I see the snackbar showing across all browser sessions, even across different machines.
#inject ISnackbar Snackbar
<MudButton OnClick="ShowSnackbar"></MudButton>
#code {
public async void ShowSnackbar()
{
Snackbar.Add($"Snackbar ID: {Guid.NewGuid()}", Severity.Normal);
}
}
How can I restrict this to show only on the session on which the button was clicked?
I copied the code directly from the MudBlazor site, expecting it to behave the way it does on their page:
As you can see, I click the Open Snackbar button here, and it only shows on 1 session.

Related

HTML5 notifications in Blazor

I'm trying to use HTML5 notifications (those displayed at the OS level) in Blazor. I can't bring up a dialog asking if the user wants to enable these notifications.
The compiler reports that it requires additional arguments (like object?[]?), but I can't find which ones. Can someone please advise me how this call should look like?
Edit: Blazor WASM + .net 6
#inject IJSRuntime JsRuntime
private async Task AllowNotif()
{
await JSRuntime.InvokeAsync<string>("Notification.requestPermission");
}

How to link a windows form in one project to a button in another project using MVC3

In my program I have a registration page where a user enters all his details to register to the system. On this page I have a button that when selected should open up a windows form that is located on another project, however when I select this button nothing is happening.
The code behind the windows form application:
public GenerateToken()
{
InitializeComponent();
}
public void Main()
{
Application.Run(GenerateToken.ActiveForm);
}
The above code is found in a project on its own. In another project this is the code for the controller that calls this windows form:
public ActionResult generateToken()
{
new TokenGenerator.GenerateToken().Main();
return RedirectToAction("RegisterPage");
}
The code in the view where the button is located for this button is:
<input id="GenToken" type="button" value="generateToken" onclick = "generateToken" />
Is there something that I am doing wrong regarding the button or is the way that I am trying to link them together wrong?
As you insist on this way, you have to create a communication channel between these two apps. You can use modern technologies for this purpose such as Named Pipes, .NET remoting or WCF hosted on IIS (better one).
If you choose Named Pipes or .NET remoting, you have to create windows service project and use it as server application. As shown in this sample, create a host object and register a channel in your windows service solution. You have to define a method in this hosted class as a runner of your windows application. Be noticed that you have to run it as process not by the way you did in your code. Your web app must call that method from server hosted object which will cause your windows app to run. How to start process is explained here. Let me know if you understand what to do.
I managed to solve this by creating a class in my windows application project and linking this class to my main mvc project. The class contained the following method:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SomeForm form1 = new SomeForm();
Application.Run(form1);
By linking this to a button in my main MVC project I was able to load the windows form on button click.

how to fill in html textbox from (c# ) windows application

I would like to do the following from a windows application:
Open a browser and navigate to a URL.
When i am at the correct URL in my browser window, i want to fill in data in the html textboxes on that page from my windows application.
When that is done i want to press a button.
Note:
Google Chrome and Mozilla Firefox is currently used in my browser Window.
Internet Explorer(IE) is not used in browser Window.
1.The URL (https://www.exple.com/login.html ) had already opened on anyone web browsers such as Mozilla Firefox and Google Chrome in local System.
It consists of Two Textboxes such as Username and Password .
2.I Want to fill in data in Username & Password Textbox on Google Chrome/Mozilla Firefox Web Browser.
3.The Input data is taken from C# Windows Application to URL When pressed Submit Button in Windows Application.
Thanks in Advance .
You can do it, but it will really hard,but if you make autoscript in testcomplete and then get code from it, code will be ugly , but working
That's what WatiN is for. It was primarily created for web application testing, but your problem can be solved with WatiN as well.
Example from the front page:
[Test]
public void SearchForWatiNOnGoogle()
{
using (var browser = new IE("http://www.google.com"))
{
browser.TextField(Find.ByName("q")).TypeText("WatiN");
browser.Button(Find.ByName("btnG")).Click();
Assert.IsTrue(browser.ContainsText("WatiN"));
}
}
This example uses IE, but Firefox is supported as well.

Problem with AddMenu Items in ASP.Net Application

I am generating menus and sub menus on the fly in my Master Page.
I have a link which points (AddMenu, and give url to the logoff page) to a generic logoff page. It is a html page. Before logging off, I make sure that the Sessions are aborted and cleared.
When I deploy this application on Servers, the application fails to come up.
To correct, I add a new aspx page. say Logout.aspx
When I click on Logout link on the master page -- I add menu item -- to point ot Logout.aspx.
In the Page_Load event of Logout.aspx, I clear the sessions and then Response.Redirect to the logoff page (which I was doing in the Master Page initially).
This case the sessions are working perfectly fine. What could be the possible reason for it?
From what I understand it sounds like you initially were trying to clear the session in the master page code-behind. This code was supposedly tied to the logoff menu item but you said the item was a 'link which points to a generic logoff page'.
My guess is you were expecting the link to trigger a postback to the masterpage when it was actually just directing the user to the html page.
When you moved the code to the Page_Load of the new Logoff.aspx page, your session clearing code was triggered correctly when they requested the Logoff.aspx page.
If this is the case, what you have found is that there is a big difference between the following
Logoff
and
<asp:LinkButton ID="linkLogoff" runat="server" Text="Logoff" />

Call a method on browser closing

I am facing an issue in my application when a user directly clicked on browser close [X] button. Browser can be IE, Chrome, Mozilla, Firefox and many more.
What I want to do:
1. as soon as user hits [X] button of browser, need to set their status as logged off in database for which we have a method in Login.aspx file which is within the master page.
2. We do not have any Logoff feature in the application
I will be thankful if anyone suggests a solution to call the method which sets the user status as logged off from master page.
Thanks in advance.
This is not possible due to the nature of http connections and the web in general. Simply have a timeout (eg. 10 minutes) after which a user gets logged out automatically.
Javascript has an onunload function, so you could do:
<body onUnload="doFunction()">
However this, and other methods are going to be unreliable (I'm not sure in which specific instances it is fired) as it would be a security concern allowing websites to have access to perform many functions on browser onunload.
The best solution would be to have cookies/sessions automatically time out, and also to educate users to logout if the system is sensitive.
If you are using jQuery you could work with
$(window).unload( function () {
$.ajax({ **your params** });
} );
But I have to agree with Tom Gullen here - your sessions should timeout eventually.

Categories