[Umbraco] Add User with hashed password & check unique loginname

protected static bool addUmbracoUser(string loginName, string userName, string password)
{
if (ensureUniqueLoginName(account))
{
User u = umbraco.BusinessLogic.User.MakeNew(userName, loginName.Trim(), hashPassword(password), new List < UserType> ());
u.addApplication("content");
u.addApplication("media");
u.addApplication("users");
u.addApplication("settings");
u.addApplication("developer");
u.addApplication("members");
u.Save();
return true;
}
else
{ return false; }
}

private static bool ensureUniqueLoginName(string loginName)
{
umbraco.BusinessLogic.User[] u = umbraco.BusinessLogic.User.getAllByLoginName(loginName);
if (u.Length != 0)
{
return false;
}
return true;
}

private static string hashPassword(string password)
{
HMACSHA1 hash = new HMACSHA1();
hash.Key = Encoding.Unicode.GetBytes(password);

string encodedPassword = Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
return encodedPassword;
}


Reference: http://our.umbraco.org/wiki/reference/api-cheatsheet/users,-user-types-and-permissions/add-user-with-hashed-password

沒有留言:

張貼留言