Asp.Net Forums is hereafter referred to as ANF, version 2.0. This article is a running account of the coefficients. It just records today's work and has no technical content.
The existing system already has its own set of user systems, which need to be connected to ANF. It is required that when existing system users register, log in, and change passwords, they must be reflected in the ANF accordingly. ANF user changes are not required to affect existing systems, only one-way synchronization.
Find a simple way to complete the integration while touching as little existing code as possible. I looked at Baoyu's CnForums integration plan with the existing system before, and finally decided to be lazy and append the user-related information and operation type to the url after the above actions of the existing system and pass it to a new page to process the related operations in the ANF.
This new page is called bridge.aspx for the time being. Of course, it must be built in an ANF project and all the using items must be used. It is responsible for receiving user name, password and other information and then performing corresponding ANF operations.
Registration
is very simple. My Username and Password are self-packaged attributes. I get information from Request. If we don’t have an Email in our system, we just add one. Next, we need to set AccountStatus to Pass and IsAnonymous to turn off. The bool parameter in Users.Create represents whether to send an email indicating successful registration to the user. Of course I turned it off if there was no email.
Note that I'm catching errors everywhere without returning them. You don't have to do this.
public void Register()
{
try
{
User user = new User();
user.Username = Username;
user.Password = Password;
user.Email = " [email protected] ";
user.AccountStatus = UserAccountStatus.Approved;
user.IsAnonymous = false;
Users.Create(user, false);
}
catch
{
}
}
The login
is basically copied and pasted. Pay attention to the setcookie at the end, which is the key to synchronous login.
public void Login()
{
try
{
User userToLogin = new User();
userToLogin.Username = Username;
userToLogin.Password = Password;
LoginUserStatus loginStatus = Users.ValidUser(userToLogin);
if (loginStatus == LoginUserStatus.Success)
{
if (!Globals.GetSiteSettings().AllowLogin)
{
bool allowed = false;
int userid = Users.FindUserByUsername(userToLogin.Username).UserID;
ArrayList roles = Roles.GetRoles(userid);
foreach (Role role in roles)
{
if (role.Name == "Site Administrators" || role.Name == "Global Administrators")
{
allowed = true;
break;
}
}
if (!allowed)
{
return;
}
}
set_Cookie(userToLogin.Username, "1");
}
}
catch
{
}
}
of changing the password
is based on the login already happening. If your assumption is not like this, you need to add your own judgment.
public void ChangePassword()
{
try
{
ForumContext forumContext = ForumContext.Current;
User user = forumContext.User;
if (user != null)
{
user.ChangePassword(Password, NewPassword);
}
}
catch
{
}
}
Set cookie
public void set_Cookie(string Username, string Selet_item)
{
if(Selet_item == "0")
{
FormsAuthentication.SetAuthCookie(Username,false);
}
else
{
ForumContext forumContext = ForumContext.Current;
FormsAuthentication.SetAuthCookie(Username,true);
forumContext.Context.Response.Cookies[FormsAuthentication.FormsCookieName].Expires=DateTime.
_Now.AddDays(System.Convert.ToInt32(Selet_item));
}
}
Finally, after registering, logging in, and modifying the existing system, the Response.Redirect page will be ok. If you don’t like Redirect, Server.Transfer, or xmlhttp will do. It depends on the display you need. Another important point is that the string you transmit must be encrypted, otherwise it cannot be written... ※¥※×%※×, if it is still not safe, use IP to determine who can access this page!