This repository has been archived by the owner on Dec 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 239
SMS Configuration
Peter Mbanugo edited this page Jul 19, 2014
·
3 revisions
In MembershipReboot for mobile phone confirmation and mobile phone two-factor authentication, SMS messages are used. This requires the hosting application to provide SMS support. This is implemented in a similar way that email messages are handled -- there is an event handler for the pertinent events.
MembershipReboot provides a SmsEventHandler base class that sends the SMS messages for the correct events, but a derived class is required to actually deliver the SMS messages. This must be implemented by the hosting application. This SmsEventHandler derived class would then need to be registered with on the AddEventHandler API on MembershipRebootConfiguration class.
Here's an example that uses twilio SMS API
public class TwilloSmsEventHandler : SmsEventHandler
{
const string sid = "";
const string token = "";
const string fromPhone = "";
public TwilloSmsEventHandler(ApplicationInformation appInfo)
: base(new SmsMessageFormatter(appInfo))
{
}
string Url
{
get
{
return String.Format("https://api.twilio.com/2010-04-01/Accounts/{0}/SMS/Messages", sid);
}
}
string BasicAuthToken
{
get
{
var val = sid + ":" + token;
var bytes = System.Text.Encoding.UTF8.GetBytes(val);
val = Convert.ToBase64String(bytes);
return val;
}
}
HttpContent GetBody(Message msg)
{
var values = new KeyValuePair<string, string>[]
{
new KeyValuePair<string, string>("From", fromPhone),
new KeyValuePair<string, string>("To", msg.To),
new KeyValuePair<string, string>("Body", msg.Body),
};
return new FormUrlEncodedContent(values);
}
protected override void SendSms(Message message)
{
if (!String.IsNullOrWhiteSpace(sid))
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", BasicAuthToken);
var result = client.PostAsync(Url, GetBody(message)).Result;
result.EnsureSuccessStatusCode();
}
}
}
var config = new MembershipRebootConfiguration();
var appinfo = new AspNetApplicationInformation("Test", "Test Email Signature",
"UserAccount/Login",
"UserAccount/Register/Confirm/",
"UserAccount/Register/Cancel/",
"UserAccount/PasswordReset/Confirm/",
"UserAccount/ChangeEmail/Confirm/");
config.AddEventHandler(new TwilloSmsEventHandler(appinfo));