YAFLogo

Posted by: ebirbal - Sunday, 23 May 2010 12:24:41
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. [b]Step 1:[/b] Generate a machine key for yourself from [url=http://www.developmentnow.com/articles/machinekey_generator.aspx]here [/url]or [url=http://www.developerfusion.com/tools/generatemachinekey/]here[/url] and update the web.config in both your applications. [b]Step 2:[/b] 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. [b]Step 3:[/b] 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) [b]Step 4:[/b] 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 [code][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); }[/code] [b]Step 5:[/b] You might also want to add code similar to the above after you have created a new user in your application [b]Step 6 (optional):[/b] In the Forums folder that you created in Step 2, create a web.config as shown below [code] [/code] 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 [code][/code] node in the above web.config. [b]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]);[/code] [b]Step 8: [/b] 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

Posted by: Mertez - Monday, 15 August 2011 16:43:25
Dears, I am trying what you did in authenticating from outside of YAF. I have read your article many times and tried to do what every you told. but i think there are some differences between our YAF versions, i did some changes to bring up the application without errors but as long as i call YafMembership webservice i give an error for missing a parameter in SQL. i tried to change your: [color=red]ForumPage currentPage = new ForumPage(); YafContext PageContext = currentPage.PageContext;[/color] bool Result = [color=red]PageContext.[/color]CurrentMembership.ValidateUser(UserName, Password); To: [color=green]YAF.Providers.Membership.YafMembershipProvider[/color] ymp = new YAF.Providers.Membership.YafMembershipProvider(); bool Result = [color=green]ymp[/color].ValidateUser(UserName, Password); in order to get rid of errors for FORUMPAGE() class. but now i'm giving the sql error below: [color=orange]System.Data.SqlClient.SqlException: Procedure or function 'yaf_prov_getuser' expects parameter '@ApplicationName', which was not supplied.[/color] i checked the source and found that the [color=blue]YAF.Providers.Membership.YafMembershipProvider.ValidateUser[/color] sends the @ApplicationName however it doesn't have it in parameters. Now. do you have any idea? have you checked the problem in 1.9.5.5 ? Morteza mertez@gmail.com

Posted by: ebirbal - Sunday, 18 September 2011 08:16:09
Hey, sorry I am not a frequent visitor to the forums, and neither did I upgrade to 1.9.5, so i was not aware of this breaking change. Anyways, I did decide to upgrade to 1.9.6 Beta and came across this issue myself. The fix though, is quiet simple really - all you need to get the current page context, is to use [code=csharp]YafContext.Current[/code] So essentially, you can get rid of the following two lines: [code=csharp]ForumPage currentPage = new ForumPage(); YafContext PageContext = currentPage.PageContext;[/code] and instead use [color=red]YafContext.Current[/color] as appropriate Please find the new [b]YafMembership.cs[/b] attached. Its my guess that the above fix would work for 1.9.5 as well (but its just a guess) Jaben and team, thanks for the awesome forums!

Posted by: jigglypuff2 - Monday, 5 March 2012 02:53:52
Thanks for this -- it got me on the right track...:) I didn't need to add another cookie though -- with the same machine key, YAF picked up the creds of the user when they signed into my system.

Posted by: jigglypuff2 - Monday, 5 March 2012 19:11:01
I just realized -- there's no reason to even pass the password when calling the create new user in YAF, because the user never logs in from YAF.

Posted by: jigglypuff2 - Monday, 5 March 2012 19:13:22
Also, while hashin the username as a shared secret is clever, it seems a little overboard to me, since the service itself is pretty much hidden via security by obscurity. And, if you have captchka's turned on for posting messages, you'll prevent spam bots as it is.

Posted by: jigglypuff2 - Monday, 5 March 2012 19:59:45
As such, I've simplified your web service to this: [code=csharp] [WebMethod] public bool IsUserRegistered(string UserName) { MembershipUser user; user = YafContext.Current.Get().GetUser(UserName,false); if (user == null) { return false; } else { return true; } } [WebMethod] public MembershipCreateStatus CreateUser(string UserName, string Password, string email, string PasswordQuestion, string PasswordAnswer) { MembershipCreateStatus Status = MembershipCreateStatus.ProviderError; if (!IsUserRegistered(UserName)) { YafContext.Current.Get().CreateUser(UserName, Password, email, PasswordQuestion, PasswordAnswer, true, null, out Status); if (!RoleMembershipHelper.IsUserInRole(UserName, "Registered")) { RoleMembershipHelper.AddUserToRole(UserName, "Registered"); } } return Status; } [/code] BTW, yeah for SOAP! :lol:

Posted by: James H - Tuesday, 20 March 2012 16:33:30
@jigglypuff2 - would it be possible to see the code you have implemented into your main application that authenticates via the web service with your new web service code? Thanks in advance

Posted by: jigglypuff2 - Tuesday, 3 April 2012 04:24:49
Sure. I generate a proxy as explained earlier in the thread (funny that VS hides "Add Web Reference" now, but it's there as explained in the earlier thread.) Then, once I have the reference, this is all I do: [code=csharp] Forums.YAFMembership Forums = new Forums.YAFMembership(); if (!Forums.IsUserRegistered(login_control.UserName)) { Forums.MembershipCreateStatus Status = Forums.CreateUser(login_control.UserName, "na", email, "na", "naa"); } [/code]

Posted by: jigglypuff2 - Tuesday, 1 May 2012 20:25:15
Oh yeah, and don't forget to add the following to app.config [code=xml] [/code]