I got some problems when writing code for user login. (Our version is 15.0.3)
We need to use code to log the user in after he/she is registered successfully as a new member.
Here is my code:
IiMISUser login_user = null;
try
{
login_user = CContactUser.LoginByWebLogin(EmailAddress.Text.Trim(), WebLogin.Text.Trim());
}
catch (UnableToLoginException)
{
// that didn't work, try it by ContactId
try
{
login_user = CContactUser.LoginByContactId(EmailAddress.Text.Trim(), WebLogin.Text.Trim());
}
catch (UnableToLoginException) {
}
}
// if one of the above worked, store the user in the session and log the user in
if (login_user != null)
{
login_user.ThrowExceptionOnError = true;
Session["LoginUser"] = login_user;
Asi.ContentManagerNet.SessionState.IsLoggedIn = true;
Asi.ContentManagerNet.SessionState.User = (Asi.iBO.ContentManagement.CWebUser)login_user;
Response.Redirect("~/Payment/PayNow_Professional.aspx");
}
// if not, give an error message
else
Msg.Text = "Invalid login id or password: Please try again";
The error happens in this statement: Asi.ContentManagerNet.SessionState.User = (Asi.iBO.ContentManagement.CWebUser)login_user;
It complains "Unable to cast object of type 'Asi.iBO.ContactManagement.CContactUser' to type 'Asi.iBO.ContentManagement.CWebUser'."
We need 'Asi.ContentManagerNet.SessionState.User' to store the user's login status for further pages, but it is 'CWebUser' type, how do we assign a CContactUser to CWebUser ?
Thanks.
Not castable
Unfortunately, you can't directly convert a CContactUser to a CWebUser. You can use CWebUser.LoginByWebLogin, which should work the same way as CContactUser.LoginByWebLogin.