Wednesday, July 30, 2008

Email Verification using c#

One thing that's really common is to require email verification before letting a new user into the system. The CreateUserWizard by default doesn't work that way, basically after user registration is done you are logged in automatically (if LoginCreatedUser=true) or if false then the user just needs to login.

As we have decided email is the central point here - working as the username, let's implement email verification before newly created users can login.

It should work this way - after the user has registered he/she should be told something like "Thanks for registration. Now wait for an email and click the link for verification, after that you have access to our site".

First, inside the CreateUserWizard, create a CompleteWizardStep template:





Thanks for registering with us

Now wait for an email to be sent to the email address you specified with instructions to enable your account and login.



Now set LoginCreatedUser="False" and DisableCreatedUser="True" for the CreateUserWizard.

This will create the user as disabled and the idea is to send the user an email containing an unique verification url which when clicked sets the user account to approved (oUser.IsApproved = true;)

In CreatedUser event we are supposed to send an email:


protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
CreateUserWizard cuw = (CreateUserWizard)sender;
MembershipUser user = Membership.GetUser(cuw.UserName);
Guid userId = (Guid)user.ProviderUserKey;
string sHeader = "";
string sBody = "";
MailHelper.NewUserMail(userId.ToString(), ref sHeader, ref sBody);
MyContext.SendEmail(cuw.Email,
sHeader,sBody, true);
}



I am using some helper functions but the basic idea is that all users gets a unique id from the provider - and for SQL Server it's a guid, which is guaranteed to be unique and also to hard to guess for an outsider. So I retrieve that value for the newly created user through user.ProviderUserKey and use that to build up a registration url string in the email:

To give you the idea the string will be

http://www.aspcode.net/blabla/activate.aspx?id=

We send an email to the user containing that string and when they click on it they go to our page activate.aspx:

In Page_Load we simply retrieve the id and approve it:


Guid oGuid = new Guid(sKey);
MembershipUser oUser = Membership.GetUser(oGuid);
if (oUser != null && oUser.IsApproved == false )
{
oUser.IsApproved = true;
Membership.UpdateUser(oUser);
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(oUser.UserName,
false);
}



Last, the verification cheme is pretty lame I know, but it might be good enough. Some improvements would be to also require the email address, which would force the user to also enter the email adrress (mimimizes guid guessing risks), and of course the code to just use user.ProviderUserKey is not very smart - it works for SQL Server but there is a risk some other provider just implements it as a autonumber field - i.e just a sequencial number, very easy to guess. You could of course add your own guid generation, storing it somewhere else - tying it with the user.ProviderUserKey.


Regards,


Ujjwal B Soni

+919998971048

C# code to POST XML Data & Fetch Return data

Hi Friends,

We usually face trouble when we want to fetch the return data after post back, especially when it is an XML Post back. I've developed a code that posts XML data to a website & grabs the return values.Below is the code to POST XML data on a particular website & fetch the return data after form post back.

try
{

ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "";
postData += "B2INT0302DEMODEMO"+TextBox1.Text.ToString()+"";
byte[] bBuffer = encoding.GetBytes(postData);

// Prepare web request...
WebRequest myRequest =
(WebRequest)WebRequest.Create("http://demo.com/default.aspx");
myRequest.Method = "POST";
myRequest.ContentType = "application/xml";
myRequest.ContentLength = bBuffer.Length;

// Send the data.
Stream newStream = myRequest.GetRequestStream();
newStream.Write(bBuffer, 0, bBuffer.Length);

myWebResponse = myRequest.GetResponse();

rawResponseStream = myWebResponse.GetResponseStream();



Encoding enc = Encoding.GetEncoding("utf-8");
StreamReader loResponseStream = new StreamReader(myWebResponse.GetResponseStream(), enc);


TextBox2.Text = loResponseStream.ReadToEnd();

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(TextBox2.Text.ToString());
XmlNodeList xmlnode1 = xmldoc.GetElementsByTagName("TAG1");
for (int i = 0; i < xmlnode1.Count; i++)
{

Session["TAG1"] = xmlnode1[i].InnerText;

}
}
catch (Exception eee)
{
Response.Write(eee.ToString());
}


Regards,

Ujjwal B Soni

Tuesday, July 29, 2008

Blackberry Development using Netbeans

Please let me know if you find any errors or missing steps.

Installation and setup:

Step 1 - download and install Suns Java SDK

Step 2 - download and install Netbeans IDE

Step 3 - download and install Netbeans Mobility Pack

Step 4 - download and install RIMs Blackberry JDE

Step 5 - Launch Netbeans, from the toolbar select tools > java platform manager > add platform and select ‘custom java micro edition platform emulator’

Step 6 - Enter (or paste) Rims JDE home directory, for example: ‘C:\Program Files\Research In Motion\BlackBerry JDE 4.2.0′ as the Platform Home, then add ‘Platform Name’ (eg. Rim) and ‘Device Name’ (eg. Blackberry)

Step 7 - Select ‘Next’, From the ‘Bootstrap Libraries’ list, remove every item except ‘net_rim_api.jar’

Step 8 - Select ‘Next’, Ignore the ‘Sources’ pane but add the path to the Blackberry javadocs, for example: ‘C:\Program Files\Research In Motion\BlackBerry JDE 4.2.0\docs\api\’ then click ‘finish’

Step 9 - Restart Netbeans

Create a simple Blackberry Application:

Step 1 - Create a new project by selecting ‘mobile application’ from the ‘mobile category’, uncheck the ‘Create Hello Midlet’ option

Step 2 - Select the Blackberry platform that you’ve just created above

Step 3 - Add this xml data to the build.xml file which is visible if you select the ‘Files’ pane

Step 4 - Next you need to create an .alx file which is a Blackberry ‘Application Loader’ xml file, in the ‘Files’ pane, right-click and select ‘new’ > ‘empty file’
Name the file the same as your application (eg. myApp.alx), and add this xml data [important - only use numerics in the version number, any letters can cause issues for OTA installing of the application when you’re done, for example do not use any ‘beta’ version notifiers like b1.0

Step 5 - You’re now ready to start writing your application, switch back to the ‘Project’ pane and create your application, native Blackberry apps extend the UiApplication class instead of Midlet so you’ll have cheat Netbeans by entering your main class as the Midlet: right click on your application in the project pane and select ‘Properties’, under ‘Application Descriptor’ > MIDLets enter the name of your class that extends UiApplication (ignore any warnings)

Step 6 - Right click on your project and select ‘Clean and Build’, once you know your application will build select ‘Debug’ from the same menu. Netbeans should spawn the ‘Rim Remote Debug Server’ and the Blackberry Simulator, all your System.out.println statements will appear in the Debug Server not in the Netbeans output pane.


that's it....

--Ujjwal B Soni