RT News

Friday, January 22, 2016

Sending username and password from ASP .Net ...

I figured out how to integrate a DNN portal with Moodle! I found an older post on this forum (http://moodle.org/mod/forum/discuss.php?d=5069) which showed how to pass the username and password to Moodle from an HTML form. In order to get the ASP.NET app to be able to post data to another form (when the values were stored in variables rather than a form), I created a VB.NET class that can post data to another form. I added a usercontrol with a linkbutton on it that calls the class and passes the username and password to the Moodle site. Here is the VB.NET class that I used: Public Class CrossPost 'Collection to hold the field names and their values Private fieldNames As System.Collections.Specialized.NameValueCollection = New System.Collections.Specialized.NameValueCollection 'Add a new field name/value pair to post the other form Public Sub AddField(ByVal name As String, ByVal value As String) fieldNames.Add(name, value) End Sub Public Sub PostDataToForm(ByVal Url As String, ByVal FormName As String, ByVal OpenLinkInNewWindow As Boolean) Try If OpenLinkInNewWindow Then Url = Url & """ target='_blank'" System.Web.HttpContext.Current.Response.Clear() System.Web.HttpContext.Current.Response.Write( "") System.Web.HttpContext.Current.Response.Write( String.Format( _ "", FormName)) System.Web.HttpContext.Current.Response.Write( String.Format( _ "
", FormName, "post", Url)) 'Add the fields/values to the response For Each value As String In fieldNames.Keys System.Web.HttpContext.Current.Response.Write( _ String.Format("", _ value, fieldNames(value))) Next System.Web.HttpContext.Current.Response.Write( "
") System.Web.HttpContext.Current.Response.Write( "") Console.WriteLine(System.Web.HttpContext.Current.Response.ToString()) System.Web.HttpContext.Current.Response.End() Catch ex As Exception Throw End Try End Sub To call the class and pass in the username and password from a DNN app you can use this code: Protected Sub lnkMoodle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkMoodle.Click Dim postToMoodle As New CrossPost() Dim mUser As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo postToMoodle.AddField( "username", mUser.Username) postToMoodle.AddField( "password", mUser.Membership.Password) postToMoodle.PostDataToForm(Url:= "http://YOUR_MOODLE_SITE/login/index.php", _ FormName:= "form", OpenLinkInNewWindow:=chkOpenInNewWindow.Checked) End Sub

No comments: