Skip to content

Instantly share code, notes, and snippets.

@justsayantan
Last active April 16, 2017 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justsayantan/135ba7618aed612bfc6ed9927d4bad67 to your computer and use it in GitHub Desktop.
Save justsayantan/135ba7618aed612bfc6ed9927d4bad67 to your computer and use it in GitHub Desktop.
Encrypt user passwords from Audience Manager Login
using Sdl.AudienceManager.ContentDelivery;
using Sdl.AudienceManager.ContentDelivery.Profile;
namespace My.Controllers
{
public class LoginController : BaseController
{
[HttpPost]
public virtual ActionResult LoginForm(FormModel model, EntityModel entity, int containerSize = 0)
{
SetupViewData(entity, containerSize);
FormModel Model = (FormModel)EnrichModel(entity);
Model.EmailId = model.EmailId;
Model.Password = model.Password;
Model.IsLoginError = IsLoggedIn(model.EmailId,model.Password);
if (!Model.IsLoginError)
{
//TODO: Need to implement the code once login done successfully.
}
return View(Model.MvcData.ViewName, Model);
}
private bool IsLoggedIn(string modelEmailId, string userPassword)
{
bool loginErrorStatus = true;
//TODO:Connect to Audience Manager & Check the Login Details
try
{
Contact contact = Contact.FromEmailQueryString(modelEmailId);
string password = contact.ExtendedDetails["Password"].StringValue;
string salt = contact.ExtendedDetails["Password_salt"].StringValue;
string hashedPassword = Encription.HashPassword(modelPassword,salt);
if (password == hashedPassword)
{
loginErrorStatus = false;
}
}
catch (Exception e)
{
loginErrorStatus = true;
}
return loginErrorStatus;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace My.Helpers
{
public static class Encription
{
public static string HashPassword(string valueToEncrypt, string token)
{
if (string.IsNullOrEmpty(token))
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(valueToEncrypt, System.Web.Configuration.FormsAuthPasswordFormat.SHA1.ToString());
}
else
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(token.ToString() + ":" + valueToEncrypt.Trim(), System.Web.Configuration.FormsAuthPasswordFormat.SHA1.ToString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment