Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How is SignalR aware of userID while EF Core Identity has been deployed as login system?

How is SignalR aware of userID while EF Core Identity has been deployed as login system?

Scheduled Pinned Locked Moved ASP.NET
javascriptasp-netdatabasetoolsjson
1 Posts 1 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Dunlop
    wrote on last edited by
    #1

    I'm using EF Core Identity as register/login system in my project. I tried to use signalR to push notification when a new message is send to a user. I have a form with two inputs, one for user who will receive the message and the other for the main message text. The message information is stored in the database and a notification is send to the client. I used the following codes: Adding signalR service to **program.cs**:

    builder.Services.AddSignalR();
    .
    .
    app.MapHub("/msgHub");

    Code for **Hub**:

    public class MessageHub : Hub
    {
    private readonly IUserRepository _userRepository;
    private readonly ICostCenterRepository _costcenterRepository;

            public MessageHub(IUserRepository userRepository, ICostCenterRepository costcenterRepository)
            {
                \_userRepository = userRepository;
                \_costcenterRepository = costcenterRepository;
            }
            public async Task SendMessage(string costCenter)
            {
                string userId = \_userRepository.GetUserIdByCostCenter(costCenter).ToString();
                await Clients.User(userId).SendAsync("ReceiveMessage");
            }
        }
    

    Code for **message.js**:

    var connection = new signalR.HubConnectionBuilder().withUrl("/msgHub").withAutomaticReconnect().build();
    connection.start();
    document.getElementById("sendBtn").addEventListener("click", function () {
    var costCenter = $("#cost-center").val();
    connection.invoke("SendMessage", costCenter).catch(function (err) {
    return console.error(err.tostring());
    });
    });

    Script in the view:

        connection.on("ReceiveMessage", function () {
        location.reload();
        });
        connection.start().catch(function (err) {
        return console.error(err.tostring());
        });
    

    Controller for sending message:

    public JsonResult Send(string inputMessageText, string costCenter)
    {
    int CurrentUserId = Convert.ToInt32(HttpContext.Session.GetString("userId"));
    int receiverId = _userRepository.GetUserIdByCostCenter(costCenter);

            if (!ModelState.IsValid)
            {
                return Json(new { success = false});
            }
            else
            {
                var newMessage = new Message()
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups