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

View File

@ -148,13 +148,8 @@ namespace MusicStore.Controllers
// Send an email with this link // Send an email with this link
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user); string code = await UserManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Context.Request.Scheme); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Context.Request.Scheme);
var email = new IdentityMessage await MessageServices.SendEmailAsync(model.Email, "Confirm your account",
{ "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
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);
#if !DEMO #if !DEMO
return RedirectToAction("Index", "Home"); return RedirectToAction("Index", "Home");
#else #else
@ -180,7 +175,7 @@ namespace MusicStore.Controllers
return View("Error"); return View("Error");
} }
var user = await SignInManager.UserManager.FindByIdAsync(userId); var user = await UserManager.FindByIdAsync(userId);
if (user == null) if (user == null)
{ {
return View("Error"); return View("Error");
@ -217,13 +212,8 @@ namespace MusicStore.Controllers
// Send an email with this link // Send an email with this link
string code = await UserManager.GeneratePasswordResetTokenAsync(user); string code = await UserManager.GeneratePasswordResetTokenAsync(user);
var callbackUrl = Url.Action("ResetPassword", "Account", new { code = code }, protocol: Context.Request.Scheme); var callbackUrl = Url.Action("ResetPassword", "Account", new { code = code }, protocol: Context.Request.Scheme);
var email = new IdentityMessage await MessageServices.SendEmailAsync(model.Email, "Reset Password",
{ "Please reset your password by clicking here: <a href=\"" + callbackUrl + "\">link</a>");
Destination = model.Email,
Subject = "Reset Password",
Body = "Please reset your password by clicking here: <a href=\"" + callbackUrl + "\">link</a>"
};
await UserManager.SendMessageAsync("Email", email);
#if !DEMO #if !DEMO
return RedirectToAction("ForgotPasswordConfirmation"); return RedirectToAction("ForgotPasswordConfirmation");
#else #else
@ -332,11 +322,29 @@ namespace MusicStore.Controllers
return View(); return View();
} }
// Generate the token and send it var user = await SignInManager.GetTwoFactorAuthenticationUserAsync();
if (!await SignInManager.SendTwoFactorCodeAsync(model.SelectedProvider)) if (user == null)
{ {
return View("Error"); 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 }); 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(); var user = await GetCurrentUserAsync();
// Generate the token and send it // Generate the token and send it
var code = await UserManager.GenerateChangePhoneNumberTokenAsync(user, model.Number); var code = await UserManager.GenerateChangePhoneNumberTokenAsync(user, model.Number);
var message = new IdentityMessage await MessageServices.SendSmsAsync(model.Number, "Your security code is: " + code);
{
Destination = model.Number,
Body = "Your security code is: " + code
};
await UserManager.SendMessageAsync("SMS", message);
return RedirectToAction("VerifyPhoneNumber", new { PhoneNumber = model.Number }); 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 // Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>(Configuration) services.AddIdentity<ApplicationUser, IdentityRole>(Configuration)
.AddEntityFrameworkStores<MusicStoreContext>() .AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders() .AddDefaultTokenProviders();
.AddMessageProvider<EmailMessageProvider>()
.AddMessageProvider<SmsMessageProvider>();
services.ConfigureFacebookAuthentication(options => services.ConfigureFacebookAuthentication(options =>
{ {

View File

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

View File

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

View File

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