.Net使用RabbitMQ實現(xiàn)短信密碼重置的操作步驟
在 C# 中使用 RabbitMQ 通過短信發(fā)送重置后的密碼到用戶的手機號上,你可以按照以下步驟進行
1.安裝 RabbitMQ 客戶端庫
首先,確保你已經(jīng)安裝了 RabbitMQ 客戶端庫。你可以通過 NuGet 包管理器來安裝:
dotnet add package RabbitMQ.Client
2.創(chuàng)建 RabbitMQ 連接和通道
創(chuàng)建一個 RabbitMQ 連接和通道,用于發(fā)送消息。
using RabbitMQ.Client;
using System;
using System.Text;
public class RabbitMQHelper
{
private readonly string _hostName;
private readonly string _userName;
private readonly string _password;
private readonly string _queueName;
public RabbitMQHelper(string hostName, string userName, string password, string queueName)
{
_hostName = hostName;
_userName = userName;
_password = password;
_queueName = queueName;
}
public void SendMessage(string message)
{
var factory = new ConnectionFactory()
{
HostName = _hostName,
UserName = _userName,
Password = _password
};
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: _queueName,
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey: _queueName,
basicProperties: null,
body: body);
Console.WriteLine($" [x] Sent {message}");
}
}
}3.創(chuàng)建短信發(fā)送服務
創(chuàng)建一個短信發(fā)送服務,用于處理從 RabbitMQ 接收到的消息并發(fā)送短信。
public class SmsService
{
public void SendSms(string phoneNumber, string message)
{
// 在這里實現(xiàn)短信發(fā)送邏輯
Console.WriteLine($"Sending SMS to {phoneNumber}: {message}");
}
}4.創(chuàng)建 RabbitMQ 消費者
創(chuàng)建一個 RabbitMQ 消費者,用于從隊列中接收消息并調(diào)用短信發(fā)送服務。
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
public class RabbitMQConsumer
{
private readonly string _hostName;
private readonly string _userName;
private readonly string _password;
private readonly string _queueName;
private readonly SmsService _smsService;
public RabbitMQConsumer(string hostName, string userName, string password, string queueName, SmsService smsService)
{
_hostName = hostName;
_userName = userName;
_password = password;
_queueName = queueName;
_smsService = smsService;
}
public void StartConsuming()
{
var factory = new ConnectionFactory()
{
HostName = _hostName,
UserName = _userName,
Password = _password
};
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: _queueName,
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
var phoneNumber = "user_phone_number"; // 從消息中解析出手機號
_smsService.SendSms(phoneNumber, message);
};
channel.BasicConsume(queue: _queueName,
autoAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}5.集成到用戶密碼重置流程
在你的用戶密碼重置流程中,生成新的密碼并將其發(fā)送到 RabbitMQ 隊列。
public class UserService
{
private readonly RabbitMQHelper _rabbitMQHelper;
public UserService(RabbitMQHelper rabbitMQHelper)
{
_rabbitMQHelper = rabbitMQHelper;
}
public void ResetPassword(Guid userId)
{
// 生成新的密碼
string newPassword = GeneratePassword();
// 將新密碼發(fā)送到 RabbitMQ 隊列
string message = $"Your new password is: {newPassword}";
_rabbitMQHelper.SendMessage(message);
// 其他邏輯,例如更新數(shù)據(jù)庫中的密碼
}
private string GeneratePassword()
{
// 生成隨機密碼的邏輯
return "random_password";
}
}6.啟動 RabbitMQ 消費者
在應用程序啟動時,啟動 RabbitMQ 消費者以處理消息。
public class Program
{
public static void Main(string[] args)
{
string hostName = "localhost";
string userName = "guest";
string password = "guest";
string queueName = "sms_queue";
var smsService = new SmsService();
var rabbitMQConsumer = new RabbitMQConsumer(hostName, userName, password, queueName, smsService);
// 啟動消費者
rabbitMQConsumer.StartConsuming();
}
}7.配置 RabbitMQ 連接信息
確保你的 RabbitMQ 連接信息(主機名、用戶名、密碼、隊列名)是正確的,并且 RabbitMQ 服務器正在運行。
示例代碼
以下是一個完整的示例代碼,展示如何使用 RabbitMQ 通過短信發(fā)送重置后的密碼到用戶的手機號上:
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
public class RabbitMQHelper
{
private readonly string _hostName;
private readonly string _userName;
private readonly string _password;
private readonly string _queueName;
public RabbitMQHelper(string hostName, string userName, string password, string queueName)
{
_hostName = hostName;
_userName = userName;
_password = password;
_queueName = queueName;
}
public void SendMessage(string message)
{
var factory = new ConnectionFactory()
{
HostName = _hostName,
UserName = _userName,
Password = _password
};
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: _queueName,
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey: _queueName,
basicProperties: null,
body: body);
Console.WriteLine($" [x] Sent {message}");
}
}
}
public class SmsService
{
public void SendSms(string phoneNumber, string message)
{
// 在這里實現(xiàn)短信發(fā)送邏輯
Console.WriteLine($"Sending SMS to {phoneNumber}: {message}");
}
}
public class RabbitMQConsumer
{
private readonly string _hostName;
private readonly string _userName;
private readonly string _password;
private readonly string _queueName;
private readonly SmsService _smsService;
public RabbitMQConsumer(string hostName, string userName, string password, string queueName, SmsService smsService)
{
_hostName = hostName;
_userName = userName;
_password = password;
_queueName = queueName;
_smsService = smsService;
}
public void StartConsuming()
{
var factory = new ConnectionFactory()
{
HostName = _hostName,
UserName = _userName,
Password = _password
};
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: _queueName,
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
var phoneNumber = "user_phone_number"; // 從消息中解析出手機號
_smsService.SendSms(phoneNumber, message);
};
channel.BasicConsume(queue: _queueName,
autoAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
public class UserService
{
private readonly RabbitMQHelper _rabbitMQHelper;
public UserService(RabbitMQHelper rabbitMQHelper)
{
_rabbitMQHelper = rabbitMQHelper;
}
public void ResetPassword(Guid userId)
{
// 生成新的密碼
string newPassword = GeneratePassword();
// 將新密碼發(fā)送到 RabbitMQ 隊列
string message = $"Your new password is: {newPassword}";
_rabbitMQHelper.SendMessage(message);
// 其他邏輯,例如更新數(shù)據(jù)庫中的密碼
}
private string GeneratePassword()
{
// 生成隨機密碼的邏輯
return "random_password";
}
}
public class Program
{
public static void Main(string[] args)
{
string hostName = "localhost";
string userName = "guest";
string password = "guest";
string queueName = "sms_queue";
var smsService = new SmsService();
var rabbitMQConsumer = new RabbitMQConsumer(hostName, userName, password, queueName, smsService);
// 啟動消費者
rabbitMQConsumer.StartConsuming();
}
}到此這篇關于 .Net使用RabbitMQ實現(xiàn)短信密碼重置的文章就介紹到這了,更多相關 .Net RabbitMQ密碼重置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
asp.net GridView中超鏈接的使用(帶參數(shù))
在GridView中,點擊鏈接列跳轉到指定頁面的實現(xiàn)代碼,需要的朋友可以參考下。2010-03-03
asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼實例
這篇文章主要介紹了asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼,結合實例形式詳細分析了asp.net基于QRCode生成二維碼的具體實現(xiàn)技巧,需要的朋友可以參考下2016-06-06
ASP.NET?MVC5實現(xiàn)文件上傳與地址變化處理(5)
這篇文章主要介紹了ASP.NET?MVC5實現(xiàn)文件上傳與地址變化處理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-09-09
.net實現(xiàn)微信公眾賬號接口開發(fā)實例代碼
這篇文章主要介紹了.net實現(xiàn)微信公眾賬號接口開發(fā)實例代碼,有需要的朋友可以參考一下2013-12-12
asp.net中Datalist使用數(shù)字分頁的實現(xiàn)方法
asp.net下Datalist使用數(shù)字分頁的實現(xiàn)代碼,需要的朋友可以參考下。2010-10-10
.Net Core導入千萬級數(shù)據(jù)至Mysql數(shù)據(jù)庫的實現(xiàn)方法
今天我們談談MySQL怎么高性能插入千萬級的數(shù)據(jù)的,討論這個問題牽扯到一個數(shù)據(jù)遷移功能,高性能的插入數(shù)據(jù),接下來通過本文給大家分享幾種實現(xiàn)方法,感興趣的朋友跟隨小編一起學習下吧2021-05-05

