Right Click Is Disabled...

Real Time Application

OTP Generator

OTP (One Time Password) Send it from any SMS Service, and how it is used.

What is OTP?

A one-time password (OTP) is an automatically generated numeric or alphanumeric string of characters that authenticates a user for a single transaction or login session.

 Why to use OTP?

An OTP is more secure than a static password, especially a user-created password, which can be weak or reused across multiple accounts.

OTPs may replace authentication login information or may be used in addition to it to add another layer of security.

[HttpPost]
public JsonResult SendOTP(string number)
{
    var status = "fail";
    if (number != "")
    {
        status = "Invalid";
        if (number.Length == 10)
        {
            string otpValue = new Random().Next(100000, 999999).ToString();
            try
            {
                ///   Sms Sending Code.
                var messageAPI = new MessageAPI();
                string smsBody = "Your OTP Number is " + otpValue 
                + " ( Sent By : XT)";
                
                //SMS Service Logic//
                string smsResult = messageAPI.SendSmsViaNewAPI(smsBody, 
                number);
                status = smsResult;
                Otp createOtp = new Otp()
                {
                    PhoneNo = number,
                    OtpValue = otpValue,
                    CreatedDate = DateTime.Now,
                    ExpiryDate = DateTime.Now.AddMinutes(10),
                    IsVerify = 0
                };
                otpManager.InsertOtp(createOtp);
                return Json(status, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
    }
    return Json(status, JsonRequestBehavior.AllowGet);
}

Why I wrote this article?

To describe how the one-time password works in a simple way and How you can use it in your application.

For sending the OTP, We can create a post method, which will have the Phone Number as input and the OTP will be sending to that 10-digit mobile number in form of a text SMS.

This Above Code will send the OTP to  Mobile Number.

Now How to Verify the OTP? Which the user will enter what has been received in the SMS.

For that We need a Verify or Validating OTP Method ,Which will check the Enter OTP is correct or not.

[HttpPost]
public JsonResult VerifyOTP(string otp)
{
    bool result = false;

    //Code to Verify the OTP
    var resultData = otpManager.VerifyOtp(otp);
    if (resultData != null && resultData.Tables[0] != null && resultData.Tables[0].Rows.Count > 0)
    {
        result = true;

        // Code to Update that the OTP which is entered is used.
        otpManager.UpdateVerifyOtp(otp);
    }
    return Json(result, JsonRequestBehavior.AllowGet);
}

The above code will verify the OTP.

OTP Generator Read More »

Overview of ASP.NET Core SignalR

What is SignalR?

SignalR is a library for asp.net developers that can be real-time web functionality to the application. SignalR is one of the most important technology for creating real-time applications or web functionality. We can use this library in real-time applications like real-time gaming, live chat

SignalR provides a simple API for creating server-to-client remote procedure calls that call JavaScript functions in client browsers from server-side .NET code. SignalR is supported by both servers and clients. Easy to use in an application. SignalR it’s one type of web socket.

Benefits of SignalR

  • Apps need high-frequency data from the server. Like social networks, live chat, live gaming, etc.
  • Real-time monitoring like instant sales updates or travel alerts
  • An application which is required actual notifications. For example, social networks, email, chat, games, travel reports, and many other apps use notifications.

Features of SignalR

  • Handles connection management automatically
  • Sends messages to connected clients simultaneously. For example a chat room
  • Sends a message to specific clients or groups of clients

Hubs

SignalR uses hubs to communicate between clients to servers. Hubs handle all client’s calls to each other. A hub is a center point in an asp.net application. Hubs continue to be the main connection point between the server and its clients. Clients can invoke methods on the hub, and the hub can invoke methods on the clients.  The hub is handle a single connection to the group connection. For example, it can send a message to a single connection, or all connections belonging to single users or to a group of connections. All Clients are connected to the hubs so no need to make connections every time.

Block Diagram

In the above diagram, we can see All clients are connected to the Hubs. So Duplex communication is established in SignalR. In Between Server and Clients are sends messages to each other via Hub connection. We can send real-time notifications via this connection.

Create a signalR hub

using Microsoft.AspNetCore.SignalR;
namespace SignalRChat.Hubs
{
    public class ChatHub : Hub
    {
        public async Task SendMessage(string user, string message)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }
    }
}

In this Hubs SendMessage method was created which is calling by connecting all clients to the application.

Call client methods from the hub

We have called the hub method using javascript client calls. So first connection defines using one of the methods of the hub connection. In this javascript client code, we have connected with the ReceiveMessage channel so whenever receive a message in this channel this client receives messages.

connection.on("ReceiveMessage", (user, message) => {
    const li = document.createElement("li");
    li.textContent = `${user}: ${mes-sage}`;
    document.getElementById("messageList").appendChild(li);
});

Configure Signal

SignalR needs to be enabled in the application. To do this, make a few changes to Startup.cs. below is highlighted code inject into Program.cs file.  Mapping chatHub class in Program.cs file so we can use an entire application.

using SignalRChat.Hubs;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddSignalR();
var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.MapHub<ChatHub>("/chatHub");
app.Run();

So This SignalR is useful in a real-time environment. we can easily send or receive messages to single users or groups of users. Most useful this SignalR in a notification, live chat, and live gaming

Overview of ASP.NET Core SignalR Read More »