CheckPasswordAsync - create and sign in user. Custom authentication
Hi,
I'm implementing custom authentication in Umbraco. My aim is to validate user credentials (username + password) against external API and, if credentials are correct, create user (if this is the first time user authenticates) and sign in user.
I'm using Umbraco's IdentityExtensions and I'm implementing IBackOfficeUserPasswordChecker interface. I've managed to done the creating user part, however, I don't know how to sign in user after creating it.
My code:
public Task<BackOfficeUserPasswordCheckerResult> CheckPasswordAsync(BackOfficeIdentityUser user, string password)
{
Log.Info("Auth logging");
AuthResponseModel authData = null;
try
{
authData = PerformExternalAuth(user.UserName, password);
}
catch(Exception ex)
{
Log.Error(ex.ToString());
return Task.FromResult(BackOfficeUserPasswordCheckerResult.FallbackToDefaultChecker);
}
var userService = ApplicationContext.Current.Services.UserService;
var userAlreadyExists = userService.GetByEmail(user.UserName) != null || userService.GetByUsername(user.UserName) != null;
if (userAlreadyExists == false)
{
Log.Info("Creating new memeber");
var userCreated = CreateUser(authData.UserName, password, authData.Email, userService);
if(userCreated == false)
{
Log.Info("Error while trying to create user");
return Task.FromResult(BackOfficeUserPasswordCheckerResult.InvalidCredentials);
}
}
return Task.FromResult(BackOfficeUserPasswordCheckerResult.ValidCredentials);
}
private bool CreateUser(string username, string password, string email, IUserService userService)
{
// Create user identity
var newUser = userService.CreateWithIdentity(username, email, password, userService.GetDefaultMemberType());
// Set hashed password
newUser.RawPasswordValue = (Membership.Providers["UsersMembershipProvider"] as UsersMembershipProvider).HashPasswordForStorage(password);
// Manage user groups
var adminGroup = userService.GetUserGroupByAlias("admin") as IReadOnlyUserGroup;
var sensitiveDataGroup = userService.GetUserGroupByAlias("sensitiveData") as IReadOnlyUserGroup;
newUser.AddGroup(adminGroup);
newUser.AddGroup(sensitiveDataGroup);
userService.Save(newUser);
return true;
}
As far as I can tell BackOfficeSignInManager could be user, but how do I initialize it? What are other ways to authenticate user after creating it?
Side note: user authentication (sign in) works for users who already exist in database.
I'm implementing custom authentication in Umbraco. My aim is to validate user credentials (username + password) against external API, a similar task as you had.
Did you find a way of doing it? Can you share with us?
The problem I'm struggling now is how to manage user groups externally? Is it possible to somehow? And what is possible in user groups functionality to be overridden?
CheckPasswordAsync - create and sign in user. Custom authentication
Hi,
I'm implementing custom authentication in Umbraco. My aim is to validate user credentials (username + password) against external API and, if credentials are correct, create user (if this is the first time user authenticates) and sign in user.
I'm using Umbraco's IdentityExtensions and I'm implementing IBackOfficeUserPasswordChecker interface. I've managed to done the creating user part, however, I don't know how to sign in user after creating it.
My code:
As far as I can tell BackOfficeSignInManager could be user, but how do I initialize it? What are other ways to authenticate user after creating it?
Side note: user authentication (sign in) works for users who already exist in database.
Thanks!
Hi Janis
I'm implementing custom authentication in Umbraco. My aim is to validate user credentials (username + password) against external API, a similar task as you had.
Did you find a way of doing it? Can you share with us?
The problem I'm struggling now is how to manage user groups externally? Is it possible to somehow? And what is possible in user groups functionality to be overridden?
Thanks,
Alex
Hi! I didn't solve the problem. You can create groups programmatically and use the code seen in my snippet to assign them to users.
is working on a reply...