Bordin
Bordin
Explore posts from servers
CC#
Created by Bordin on 5/14/2024 in #help
Uploads folder
No description
11 replies
CC#
Created by Bordin on 5/5/2024 in #help
✅ SignalR Authorized Connection
namespace LebUpwork.Api.Hubs
{
//[Authorize] <- This causes error when connecting
public class NotificationHub : Hub<INotification>
{
public override async Task OnConnectedAsync()
{
await Clients.Client(Context.ConnectionId).ReceiveNotification($"Thank you for connecting {Context.User?.Identity.Name}");
await base.OnConnectedAsync();
}
}

}
namespace LebUpwork.Api.Hubs
{
//[Authorize] <- This causes error when connecting
public class NotificationHub : Hub<INotification>
{
public override async Task OnConnectedAsync()
{
await Clients.Client(Context.ConnectionId).ReceiveNotification($"Thank you for connecting {Context.User?.Identity.Name}");
await base.OnConnectedAsync();
}
}

}
hello, i am initiating a connection on this Hub, however when i add [Authorized] the connection is failing
const connection = new HubConnectionBuilder()
.withUrl("https://localhost:7170/Hubs/NotificationHub", {
accessTokenFactory: () => token,
skipNegotiation: true,
transport: HttpTransportType.WebSockets,
})
.build();
const connection = new HubConnectionBuilder()
.withUrl("https://localhost:7170/Hubs/NotificationHub", {
accessTokenFactory: () => token,
skipNegotiation: true,
transport: HttpTransportType.WebSockets,
})
.build();
even tho I am adding the accessTokenFactory. Am i doing something wrong?
10 replies
CC#
Created by Bordin on 3/24/2024 in #help
SignalR
Hi, I am trying to create a Notification feature for my application and I figured it should be Real-time. Which is why i am trying to use SignalR but i don't quite understand it. I made a notification hub
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
namespace LebUpwork.Api.Hubs
{
public class NotificationHub : Hub
{
public async Task SendNotification(string user, string message)
{
await Clients.All.SendAsync("ReceiveNotification", user, message);
}
}
}
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
namespace LebUpwork.Api.Hubs
{
public class NotificationHub : Hub
{
public async Task SendNotification(string user, string message)
{
await Clients.All.SendAsync("ReceiveNotification", user, message);
}
}
}
So, i really dont understand what a hub is and i have read and asked chatgpt but i can't figure it out. What's a user? what's a message? how does it know who the client is? i really dont understand anything I added the hub in program.cs btw so that step is done. I just dont know how that is supposed to send notifications because apparently it shouldn't return anything.
10 replies
CC#
Created by Bordin on 2/5/2024 in #help
Models&sql
No description
56 replies
CC#
Created by Bordin on 2/3/2024 in #help
Securing API with jwt
No description
27 replies
CC#
Created by Bordin on 7/29/2023 in #help
✅ file path not saving properly with id
[HttpPost("")]
public async Task<ActionResult<CompanyResource>> CreateCompany([FromForm] SaveCompanyResource saveCompanyResource)
{
var validator = new SaveCompanyResourceValidator();
var validationResult = await validator.ValidateAsync(saveCompanyResource);
var uploadsFolderPath = "Uploads/";
if (!validationResult.IsValid)
return BadRequest(validationResult.Errors); // this needs refining, but for demo it is ok

var companyToCreate = _mapper.Map<SaveCompanyResource, Company>(saveCompanyResource);
if (saveCompanyResource.Logo != null && saveCompanyResource.Logo.Length > 0)
{
// Generate the filename using the CompanyId or any other unique identifier
var fileName = companyToCreate.CompanyId.ToString() + Path.GetExtension(saveCompanyResource.Logo.FileName);
var filePath = Path.Combine(uploadsFolderPath, fileName);

using (var stream = new FileStream(filePath, FileMode.Create))
{
await saveCompanyResource.Logo.CopyToAsync(stream);
}

// Set the file path in the companyToCreate object to be "Uploads/companyId.jpg"
companyToCreate.Logo = filePath.ToString();
}
var newCompany = await _companyService.CreateCompany(companyToCreate);

var company = await _companyService.GetCompanyById(newCompany.CompanyId);

var companyResource = _mapper.Map<Company, CompanyResource>(company);

return Ok(companyResource);
}
[HttpPost("")]
public async Task<ActionResult<CompanyResource>> CreateCompany([FromForm] SaveCompanyResource saveCompanyResource)
{
var validator = new SaveCompanyResourceValidator();
var validationResult = await validator.ValidateAsync(saveCompanyResource);
var uploadsFolderPath = "Uploads/";
if (!validationResult.IsValid)
return BadRequest(validationResult.Errors); // this needs refining, but for demo it is ok

var companyToCreate = _mapper.Map<SaveCompanyResource, Company>(saveCompanyResource);
if (saveCompanyResource.Logo != null && saveCompanyResource.Logo.Length > 0)
{
// Generate the filename using the CompanyId or any other unique identifier
var fileName = companyToCreate.CompanyId.ToString() + Path.GetExtension(saveCompanyResource.Logo.FileName);
var filePath = Path.Combine(uploadsFolderPath, fileName);

using (var stream = new FileStream(filePath, FileMode.Create))
{
await saveCompanyResource.Logo.CopyToAsync(stream);
}

// Set the file path in the companyToCreate object to be "Uploads/companyId.jpg"
companyToCreate.Logo = filePath.ToString();
}
var newCompany = await _companyService.CreateCompany(companyToCreate);

var company = await _companyService.GetCompanyById(newCompany.CompanyId);

var companyResource = _mapper.Map<Company, CompanyResource>(company);

return Ok(companyResource);
}
I am creating a file with path Uploads/companyId.png However. The companyId is 0 because i am creating newCompany after i create the path. And if i created it before it saves as Microsoft.AspNetCore.http... And i guess that's because i created the company with no path. any idea how i can fix this? its so confusing
10 replies