Developer forum

Forum » Development » Login user in code

Login user in code

Vilhjalmur Ingthorsson
Vilhjalmur Ingthorsson
Reply

I'm creating a custom login method and trying to login user with code:

// querying the user

User user = Dynamicweb.Security.UserManagement.User.GetUserBySql("SELECT * FROM AccessUser where AccessUserExternalId = '3005013299'");

 

is there any way i can use this User object to log the user in? like using something like Dynamicweb.Frontend.LogOnHandler?

I´m using DW 9.4

 

 


Replies

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi

Currently it is not possible to login users using the API without the username and password directly - or just a user object (for security reasons)

You can do something like this instead:
https://doc.dynamicweb.com/forum/cms-standard-features/cms-standard-features/share-login-between-2-different-areas

Let me hear if that can be used.

BR Nicolai

Votes for this answer: 1
 
Nicolai Pedersen
Reply

Forgot:

You can actually Logon the user like this:

Dynamicweb.Frontend.LogOnHandler.LogOnWithToken(User.Username, Dynamicweb.Frontend.LoginHandler.GetLoginToken(User.Username, User.Password)

 
Vilhjalmur Ingthorsson
Vilhjalmur Ingthorsson
Reply

I can´t find the LogOnWithToken function, i get the error:

'LogOnHandler' does not contain a definition for 'LogOnWithToken'

 

but i was able to get it working by using this code :)

string userToken = Dynamicweb.Frontend.LogOnHandler.GetLoginToken(user.UserName, user.Password);

return Redirect("/?username=" + user.UserName + "&PwToken=" + userToken);

 
Nicolai Pedersen
Reply

Just checked - LogOnWithToken is available in 9.4.17 - and it is not static, so you need to create an instance of LogOnHandler

Sorry - hope this clarifies!

BR Nicolai

 
Nicolai Pedersen
Reply

And if you use the redirect, remember to urlencode username and password so it will work for all characters.

BR Nicolai

 
Vilhjalmur Ingthorsson
Vilhjalmur Ingthorsson
Reply

ok Thanks, i will use LogOnWithToken instead

 

You must be logged in to post in the forum