FacePalmGamer
FacePalmGamer
CC#
Created by FacePalmGamer on 8/13/2024 in #help
Issue with httpContext and cascadingParamter in blazor web app
I am attempting to mkae a login page on a blazor maui hybrid web app (new to the .NET 9.0 preview 5) Essentially I am just working on the webapp side and ignoring maui just trying to get this to work. I am following this tutorial but I have an issue on the login page. He makes a httpContext with the attribute (i think thats what those are called in the []) of cascading parameter. I dont see where he ever initilizes it (i dont think he does) so when he calls it later it errors saying its null here is the important code in the login page:
@code {
[CascadingParameter]
public HttpContext httpContext { get; set; }

[SupplyParameterFromForm]
public LoginViewModel Model { get; set; } = new();

private string? errorMessage;

private async Task Authenticate()
{
var userAccount = appDbContext.UserAccounts.Where(x => x.UserName == Model.UserName).FirstOrDefault();
if(userAccount is null || userAccount.Password != Model.Password)
{
errorMessage = "Invalid User Name or Password";
return;
}

var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, Model.UserName),
new Claim(ClaimTypes.Role, userAccount.Role)
};

var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);

await httpContext.SignInAsync(principal);
navigationManager.NavigateTo("/");

}
}
@code {
[CascadingParameter]
public HttpContext httpContext { get; set; }

[SupplyParameterFromForm]
public LoginViewModel Model { get; set; } = new();

private string? errorMessage;

private async Task Authenticate()
{
var userAccount = appDbContext.UserAccounts.Where(x => x.UserName == Model.UserName).FirstOrDefault();
if(userAccount is null || userAccount.Password != Model.Password)
{
errorMessage = "Invalid User Name or Password";
return;
}

var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, Model.UserName),
new Claim(ClaimTypes.Role, userAccount.Role)
};

var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);

await httpContext.SignInAsync(principal);
navigationManager.NavigateTo("/");

}
}
the await httpContext line is erroring
1 replies
CC#
Created by FacePalmGamer on 8/9/2024 in #help
MAUI with blazor hybrid new project not formatted
No description
1 replies
CC#
Created by FacePalmGamer on 8/29/2023 in #help
✅ Making custom Events
I am working with a pi using the System.Device.Gpio. is there a way to create an event for when a pin changes? I have never worked with making events only ever subscribed to known events, so i don't know if you even can, but I dont see why you couldn't. the GPIO library has 2 functions. waitForEvent, and waitForEventAsync. Previously I just made a task list like this: (This is a poor rendition, these functions would do alot more)
void async main()
{
List<task> tasks = new List<Task>();
tasks.add(ListenAsync);
tasks.add(Listen2Async);
await Task.WhenAll(Tasks)
}

async Task ListenAsync()
{
return controller.WaitForEventAsync(...)
}
void async main()
{
List<task> tasks = new List<Task>();
tasks.add(ListenAsync);
tasks.add(Listen2Async);
await Task.WhenAll(Tasks)
}

async Task ListenAsync()
{
return controller.WaitForEventAsync(...)
}
this seems wrong, and if I could just make a pinChange event and subscribe a method to it, it would be alot easier. Effectively like an interrupt. TLDR What is the correct way to look for pin chages? should i just stick with making a list of tasks, or is there a way to make an event system for pin changes?
8 replies
CC#
Created by FacePalmGamer on 8/24/2023 in #help
✅ Compile with bat file
Is there an easy way to compile and run in a bat file? I have a discord bot I am messing around with, and I am trying to make it redeploy itself whenever a github push is made. I can already detect when a push is made by just looking for the webhook post in a channel, then I know how to pull from git in batch, but I'm not sure how to compile the code then start the program. I read something about the csc command, but I'm not sure how to install that on my pc or use it really
5 replies
CC#
Created by FacePalmGamer on 7/19/2023 in #help
✅ Correct way to pass data between threads?
I have an event on a simple Winform reading from the serial port. The issue is I would like to output the data onto a winform component, but because its on an even it happens on a different thread and I get the error Cross-thread operation not valid: Control 'textBoxRecieve' accessed from a thread other than the thread it was created on. so what is the correct way to pass this data? I know I could do something like a channel do make a cross thread communication system, but that seems excessive and I figure there is an easier way I do now know
public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
textBoxRecieve.Text = textBoxRecieve.Text + Environment.NewLine + ((SerialPort)sender).ReadExisting();
}
public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
textBoxRecieve.Text = textBoxRecieve.Text + Environment.NewLine + ((SerialPort)sender).ReadExisting();
}
That was the function running and erroring. I was just trying to make a simple ECHO test debugListen.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); and that is how I was setting up the event
80 replies
CC#
Created by FacePalmGamer on 11/8/2022 in #help
Getting the result from a list of tasks
Its been a minute since I have done async methods but i know i can do a list of tasks I just think I'm using them wrong.
const double LAT_MIN = 37.70649; //y
const double LAT_MAX = 37.77436;
const double LON_MAX = -85.95120;
const double LON_MIN = -85.86829;

List<Task> tasks = new List<Task>();
List<Point> points = new List<Point>();

for (double i = LAT_MIN; i < LAT_MIN; i += .00005)
{
tasks.Add(CreatePointsAsync(i));
}
foreach (Task task in tasks)
task.Start();

Task.WhenAll(tasks).Wait();
foreach(Task<List<Point>> task in tasks)
{
points.InsertRange(points.Count, task.Result);
}

foreach(Point point in points)
{
Console.WriteLine(point.X + " " + point.Y);
}

async Task<List<Point>> CreatePointsAsync(double y)
{
List<Point> value = new List<Point>();
for (double i = LON_MIN; i < LON_MAX; i += .00005)
{
value.Add(new Point(i, y));
}
return value;
}

public struct Point
{
public double X;
public double Y;

public Point(double X, double Y)
{
this.X = X;
this.Y = Y;
}
};
const double LAT_MIN = 37.70649; //y
const double LAT_MAX = 37.77436;
const double LON_MAX = -85.95120;
const double LON_MIN = -85.86829;

List<Task> tasks = new List<Task>();
List<Point> points = new List<Point>();

for (double i = LAT_MIN; i < LAT_MIN; i += .00005)
{
tasks.Add(CreatePointsAsync(i));
}
foreach (Task task in tasks)
task.Start();

Task.WhenAll(tasks).Wait();
foreach(Task<List<Point>> task in tasks)
{
points.InsertRange(points.Count, task.Result);
}

foreach(Point point in points)
{
Console.WriteLine(point.X + " " + point.Y);
}

async Task<List<Point>> CreatePointsAsync(double y)
{
List<Point> value = new List<Point>();
for (double i = LON_MIN; i < LON_MAX; i += .00005)
{
value.Add(new Point(i, y));
}
return value;
}

public struct Point
{
public double X;
public double Y;

public Point(double X, double Y)
{
this.X = X;
this.Y = Y;
}
};
I need to create a lot of points so i wanted to split it up to make it faster
23 replies