I am brand new to YAF, and love it! Its a great piece of work!
I had to integrate YAF with my existing application that already used ASP .Net membership. I checked other forum posts on the subject and also the Wiki articles, and it seems to me that they are a bit dated and not compatible with the new provider model based authentication, so I rolled my own.
My approach basically involves creating a web service to handle authentication and user creation. I chose this approach because I wanted to keep YAF separate from my application (much easier to upgrade), yet wanted users to have a seamless experience between the two applications. With this approach, the forums would be hosted in a separate sub domain.
Step 1:
Generate a machine key for yourself from here or here and update the web.config in both your applications.
Step 2:
Open YAF solution in Visual Studio and create a new folder under YetAnotherForum.Net website. Within this folder, create a new Web Service, with the name YafMembership.asmx. Paste the code from the attached file YafMembership.cs into it.
Step 3:
In your main application, add a web reference to this newly created web service with the name 'Forums'
Note1: be sure to add a web reference and not a service reference.
Note2: depending of whether your main application is a web project or a website, the actual process of adding a web reference might vary a little - please check MSDN for help)
Step 4:
In your main application, paste the following code where you authenticate your user. This code should follow after you have authenticated the user. You will have to modify this code (especially the parts enclosed with
[h]attributes[/h][/code]) so that its relevant to your application.
[code]bool CreateForumsTicket = false;
Forums.AuthHeader AuthHeader = new Forums.AuthHeader();
AuthHeader.SecretKey = FormsAuthentication.HashPasswordForStoringInConfigFile([h]UserName[/h], "md5");
Forums.YafMembership Forums = new Forums.YafMembership();
Forums.AuthHeaderValue = AuthHeader;
if (Forums.Login([h]UserName[/h], [h]Password[/h]))
{
CreateForumsTicket = true;
}
else
{
[h]MembershipUser user = Membership.GetUser(UserName);
Forums.MembershipCreateStatus Status = Forums.CreateUser(UserName, Password, UserEmail, PasswordQuestion, PasswordAnswer) ;[/h]
if (Status == Forums.MembershipCreateStatus.Success)
CreateForumsTicket = true;
else{
[h]// Add the code to log the failure using whatever error logging mechanism you use[/h]
}
}
if (CreateForumsTicket)
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(30), false, "", "/");
string strEncTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie authCookie = new HttpCookie(".YAFNET_Authentication", strEncTicket);
authCookie.Path = "/";
authCookie.Domain = [h]"YourDomain.com"[/h];
HttpContext.Current.Response.Cookies.Add(authCookie);
}
Step 5:
You might also want to add code similar to the above after you have created a new user in your application
Step 6 (optional):
In the Forums folder that you created in Step 2, create a web.config as shown below
<configuration>
<system.web>
<webServices>
<protocols>
<remove name="HttpPost" />
<remove name="HttpGet" />
<remove name="Documentation" />
<add name="HttpPostLocalhost" />
</protocols>
</webServices>
</system.web>
</configuration>
This web.config removes unnecessary protocols and documentation about the web service to prevent automatic sign ups. This is in addition to the hashed SecretKey exchanged in the web service header.
Note: if you ever need to re-generate the proxy class created in step 3, make sure you comment out the
<remove name="Documentation" />[/code] node in the above web.config.
🅱Step 7:[/b]
Go to the YAF admin area, Host Settings -> Login/Registration Settings and check 'Disable New Registrations'. This will prevent the 'Register' link from appearing. However, the user can still manually enter the registration URL and register - in order to prevent that, open YetAnotherForum.Net -> Pages -> Register.ascx.cs and in the Page_Load event redirect the user to the registration URL of your main site
[code]Response.Redirect([h]"YourDomain.com"[/h]);
Step 8:
Go to the YAF admin area, Host Settings -> Login/Registration Settings and modify 'Custom Login Redirect Url' so that users are directed to your main site for logging in.
That's about it.
(pauses)
You're welcome
Edited by user
14 years ago |
Reason: Not specified