I am creating a WCF service in one of my DNN modules replacing an existing SOAP based service.
I used wcf.exe to create to create the service from the old wsdl file by following the blog.
https://waelmohamed.wordpress.com/2013/06/10/generate-net-web-service-from-wsdl-file-identical-best-practices/
I need to make the SOAP service statefull by manging the login in the server.
I did some research and created a method to send the “.DOTNETNUKE” cookie which is used to manage user login in DNN using the the following code
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
var loginStatus = new UserLoginStatus();
var portalname = "";
var objUser = UserController.ValidateUser(0, request.Body.Email,request.Body.Password, "", portalname, ip, ref loginStatus);
if (loginStatus != UserLoginStatus.LOGIN_FAILURE || loginStatus != UserLoginStatus.LOGIN_USERNOTAPPROVED)
{
UserController.UserLogin(0, objUser, portalname, ip, true);
}
var cookie=FormsAuthentication.GetAuthCookie(objUser.Username, true);
WebOperationContext.Current.OutgoingResponse.Headers.Add($"Set-Cookie: {cookie.Name}={cookie.Value}; expires={cookie.Expires}; path={cookie.Path}; {(cookie.HttpOnly?"HttpOnly":"")}");
return new UserAuthenticationResponse(new UserAuthenticationResponseBody() { Token = cookie.Value });
The cookie is then sent along with the subsequent SOAP requests.
I use the following code to get the user name from the cookie;
var cookie = WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.Cookie];
cookie= cookie.Replace(".DOTNETNUKE=", "");
var temp= FormsAuthentication.Decrypt(cookie);
var userName= temp.Name;
UserController.Instance.GetCurrentUserInfo
var user = UserController.GetUserByName(0, userName);
Is this a valid method or is there a better method to get the user id from the cookie.
I also want to set dotetnuke variables like UserController.Instance.GetCurrentUserInfo
So that I can use it in my business logic. Is there any way to do that?