React to identity changes

This commit is contained in:
Hao Kung 2015-03-05 14:23:12 -08:00
parent be747a63ed
commit 28bfb92e63
11 changed files with 73 additions and 124 deletions

View File

@ -1,41 +0,0 @@
using Microsoft.AspNet.Identity;
using MusicStore.Models;
using System.Threading.Tasks;
using System.Threading;
namespace MusicStore
{
public class EmailMessageProvider : IIdentityMessageProvider
{
public string Name
{
get
{
return "Email";
}
}
public Task SendAsync(IdentityMessage message, CancellationToken cancellationToken = default(CancellationToken))
{
// Plug in your service
return Task.FromResult(0);
}
}
public class SmsMessageProvider : IIdentityMessageProvider
{
public string Name
{
get
{
return "SMS";
}
}
public Task SendAsync(IdentityMessage message, CancellationToken cancellationToken = default(CancellationToken))
{
// Plug in your service
return Task.FromResult(0);
}
}
}

View File

@ -0,0 +1,20 @@
using System.Threading.Tasks;
namespace MusicStore.Spa
{
public static class MessageServices
{
public static Task SendEmailAsync(string email, string subject, string message)
{
// Plug in your email service
return Task.FromResult(0);
}
public static Task SendSmsAsync(string number, string message)
{
// Plug in your sms service
return Task.FromResult(0);
}
}
}

View File

@ -1,14 +1,12 @@
using Microsoft.AspNet.Authorization;
using AutoMapper;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using AutoMapper;
using MusicStore.Apis;
using MusicStore.Models;
using MusicStore.Spa.Infrastructure;
namespace MusicStore.Spa
@ -46,9 +44,7 @@ namespace MusicStore.Spa
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>(Configuration)
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders()
.AddMessageProvider<EmailMessageProvider>()
.AddMessageProvider<SmsMessageProvider>();
.AddDefaultTokenProviders();
// Add application services to the service container
//services.AddTransient<IModelMetadataProvider, BuddyModelMetadataProvider>();

View File

@ -148,13 +148,8 @@ namespace MusicStore.Controllers
// Send an email with this link
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Context.Request.Scheme);
var email = new IdentityMessage
{
Destination = model.Email,
Subject = "Confirm your account",
Body = "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"
};
await UserManager.SendMessageAsync("Email", email);
await MessageServices.SendEmailAsync(model.Email, "Confirm your account",
"Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
#if !DEMO
return RedirectToAction("Index", "Home");
#else
@ -180,7 +175,7 @@ namespace MusicStore.Controllers
return View("Error");
}
var user = await SignInManager.UserManager.FindByIdAsync(userId);
var user = await UserManager.FindByIdAsync(userId);
if (user == null)
{
return View("Error");
@ -217,13 +212,8 @@ namespace MusicStore.Controllers
// Send an email with this link
string code = await UserManager.GeneratePasswordResetTokenAsync(user);
var callbackUrl = Url.Action("ResetPassword", "Account", new { code = code }, protocol: Context.Request.Scheme);
var email = new IdentityMessage
{
Destination = model.Email,
Subject = "Reset Password",
Body = "Please reset your password by clicking here: <a href=\"" + callbackUrl + "\">link</a>"
};
await UserManager.SendMessageAsync("Email", email);
await MessageServices.SendEmailAsync(model.Email, "Reset Password",
"Please reset your password by clicking here: <a href=\"" + callbackUrl + "\">link</a>");
#if !DEMO
return RedirectToAction("ForgotPasswordConfirmation");
#else
@ -332,11 +322,29 @@ namespace MusicStore.Controllers
return View();
}
// Generate the token and send it
if (!await SignInManager.SendTwoFactorCodeAsync(model.SelectedProvider))
var user = await SignInManager.GetTwoFactorAuthenticationUserAsync();
if (user == null)
{
return View("Error");
}
// Generate the token and send it
var code = await UserManager.GenerateTwoFactorTokenAsync(user, model.SelectedProvider);
if (string.IsNullOrWhiteSpace(code))
{
return View("Error");
}
var message = "Your security code is: " + code;
if (model.SelectedProvider == "Email")
{
await MessageServices.SendEmailAsync(await UserManager.GetEmailAsync(user), "Security Code", message);
}
else if (model.SelectedProvider == "Phone")
{
await MessageServices.SendSmsAsync(await UserManager.GetPhoneNumberAsync(user), message);
}
return RedirectToAction("VerifyCode", new { Provider = model.SelectedProvider, ReturnUrl = model.ReturnUrl, RememberMe = model.RememberMe });
}

View File

@ -87,13 +87,7 @@ namespace MusicStore.Controllers
var user = await GetCurrentUserAsync();
// Generate the token and send it
var code = await UserManager.GenerateChangePhoneNumberTokenAsync(user, model.Number);
var message = new IdentityMessage
{
Destination = model.Number,
Body = "Your security code is: " + code
};
await UserManager.SendMessageAsync("SMS", message);
await MessageServices.SendSmsAsync(model.Number, "Your security code is: " + code);
return RedirectToAction("VerifyPhoneNumber", new { PhoneNumber = model.Number });
}

View File

@ -1,40 +0,0 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
namespace MusicStore
{
public class EmailMessageProvider : IIdentityMessageProvider
{
public string Name
{
get
{
return "Email";
}
}
public Task SendAsync(IdentityMessage message, CancellationToken cancellationToken = default(CancellationToken))
{
// Plug in your service
return Task.FromResult(0);
}
}
public class SmsMessageProvider : IIdentityMessageProvider
{
public string Name
{
get
{
return "SMS";
}
}
public Task SendAsync(IdentityMessage message, CancellationToken cancellationToken = default(CancellationToken))
{
// Plug in your service
return Task.FromResult(0);
}
}
}

View File

@ -0,0 +1,20 @@
using System.Threading.Tasks;
namespace MusicStore
{
public static class MessageServices
{
public static Task SendEmailAsync(string email, string subject, string message)
{
// Plug in your email service
return Task.FromResult(0);
}
public static Task SendSmsAsync(string number, string message)
{
// Plug in your sms service
return Task.FromResult(0);
}
}
}

View File

@ -49,9 +49,7 @@ namespace MusicStore
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>(Configuration)
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders()
.AddMessageProvider<EmailMessageProvider>()
.AddMessageProvider<SmsMessageProvider>();
.AddDefaultTokenProviders();
services.ConfigureFacebookAuthentication(options =>
{

View File

@ -62,9 +62,7 @@ namespace MusicStore
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>(Configuration)
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders()
.AddMessageProvider<EmailMessageProvider>()
.AddMessageProvider<SmsMessageProvider>();
.AddDefaultTokenProviders();
// Add MVC services to the services container
services.AddMvc();

View File

@ -51,9 +51,7 @@ namespace MusicStore
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>(Configuration)
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders()
.AddMessageProvider<EmailMessageProvider>()
.AddMessageProvider<SmsMessageProvider>();
.AddDefaultTokenProviders();
// Add MVC services to the services container
services.AddMvc();

View File

@ -70,9 +70,7 @@ namespace MusicStore
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>(Configuration)
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders()
.AddMessageProvider<EmailMessageProvider>()
.AddMessageProvider<SmsMessageProvider>();
.AddDefaultTokenProviders();
services.ConfigureFacebookAuthentication(options =>
{