Encrypt & Decrypt your AJAX traffic -Microsoft Silverlight and Ajax.NET Professional

23. February 2010

Author: Aamir Hasan

It is possible to put an AJAX token. With Silverlight we get the System.Security.Cryptography name space on the client – before it was not very easy to encrypt a string without any plug-in (well, there are some implementations of Blowfish available in JavaScript).

Encrypt and Decrypt method given below,how to implement cryptography in Ajax.NET Professional. 

public override string Encrypt(string json)
{
    return Security.Encrypt(Security.GetHashKey("hans"), json);
}

public override string Decrypt(string jsoncrypt)
{
    return Security.Decrypt(Security.GetHashKey("hans"), jsoncrypt);
}

 

With Silverlight youre able to use the html bridge between JavaScript and managed .NET code. Youll find an example in the Silverlight Quickstarts. The Silverlight C# code looks likes this:

public override string ClientScript
    {
        get
        {
            return @"AjaxPro.cryptProvider = {
        encrypt : function(s) {
            var SLPlugin = document.getElementById(""Xaml1"");
            var strOut = SLPlugin.Content.SL_SMT.Encrypt(s);
            return strOut;
        },
        decrypt : function(s) {
            var SLPlugin = document.getElementById(""Xaml1"");
            var strOut = SLPlugin.Content.SL_SMT.Decrypt(s);
            return strOut;
        }
    };";
        }
    }

public class ScriptableManagedType
{
// Note: static methods are not allowed

[ScriptableMember]
public string Encrypt(string text)
{
return Security.Encrypt(Security.GetHashKey("hans"), text);
}

[ScriptableMember]
public string Decrypt(string text)
{
return Security.Decrypt(Security.GetHashKey("hans"), text);

client-side code youre able to encrypt strings in JavaScript with the help of System.Security. In the AjaxSecurityProvider created above you have to override the ClientScript property to let Ajax.NET Professional encrypt the JSON strings using Silverlight:

 

 

 



Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net , , ,

Client MAC address

21. February 2010


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WMI Scripting HTML</title>


<script FOR="foo" EVENT="OnCompleted(hResult, 
pErrorObject,
pAsyncContext)" LANGUAGE="JScript">

            
document.forms[0].txtMACAddr.value=unescape(MACAddr);
            
document.forms[0].txtIPAddr.value=unescape(IPAddr);
            
document.forms[0].txtDNSName.value=unescape(sDNSName);
            
document.formbar.submit();
        </script>
<script
FOR="foo" EVENT="OnObjectReady(objObject,
objAsyncContext)"
LANGUAGE="JScript">

         if(objObject.IPEnabled != null
&& objObject.IPEnabled !
= "undefined" &&
objObject.IPEnabled == true)
                  {

                  
if(objObject.MACAddress != null &&
objObject.MACAddress !=
"undefined")
                         MACAddr =
objObject.MACAddress;


                  
if(objObject.IPEnabled && objObject.IPAddress(0) !=
null
&& objObject.IPAddress(0) != "undefined")
                        
IPAddr = objObject.IPAddress(0);

                  
if(objObject.DNSHostName != null &&
objObject.DNSHostName !=
"undefined")
                      sDNSName =
objObject.DNSHostName;

                   }
       
</script>


</head>
<body>
<p>



<FONT color="red"><span
ID="info"> </span>. </FONT>
<object classid="CLSID:76A64158-CB41-11D1-8B02-
00600806D9B6" id="locator" VIEWASTEXT>
</object>
<object classid="CLSID:75718C9A-F029-11d1-A1AC-
00C04FB6C223" id="foo">
</object>

<script LANGUAGE="JScript">

             var service = locator.ConnectServer();
             var MACAddr ;
             var IPAddr ;
             var DomainAddr;
             var sDNSName;
             service.Security_.ImpersonationLevel=3;
             service.InstancesOfAsync
(foo, 'Win32_NetworkAdapterConfiguration');
        </script>
</p>


<form method="POST" action="NICPost.asp" id="formfoo" name="formbar">

<input type="hidden"  name="txtMACAddr">
<input type="hidden"  name="txtIPAddr">
<input type="hidden"  name="txtDNSName">

</form>


</body>
</html>

*************************
This is the second file. It is an ASP page, but you could change it to
plain HTML and use Java to retrieve the form field values, if you
wanted.

NICPost.asp

<HTML>
<HEAD>
</HEAD>
<BODY>
Network Interface Card Information Page
<BR>
<BR>
<BR>
You are at IP Address <STRONG>
<%=request.form("txtIPAddr")%>
</STRONG>
<BR>
Your MAC address on your network card is <STRONG>
<% =request.form("txtMACAddr")%>
</STRONG>
<BR>
Your DNS Host name is <STRONG>
<% =request.form("txtDNSName")%>
</STRONG>
<BR>
<BR>
<BR>
To confirm your IP and MAC address information, go to
the command prompt and
type in
<BR>
<BR>
IPCONFIG/ALL
<BR>
<BR>
ASP reports that your IP Address is <STRONG>
<%
response.Write Request.Servervariables("REMOTE_ADDR")
%>
</STRONG>which is your external WAN IP address that
anyone can see,
<BR>
but maybe be shared by hundreds of users if you use Net
Address Translation
(NAT)
<BR>
through a common router.
</BODY>
</HTML

 


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

, ,

Client MAC address

21. February 2010

ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc = mc.GetInstances();
        string MACAddress = String.Empty;
       
        foreach (ManagementObject mo in moc)
        {
             if (MACAddress == String.Empty)
            {
                if ((bool)mo["IPEnabled"] == true)
                {
                    MACAddress = mo["MacAddress"].ToString();
                }
            }
       }

But it returns the Mac Address of the Server and not the client.

As to get this information from client end, i opted for the following javascript:

<script language="javascript">
     function showMacAddress(){
 
    var obj = new ActiveXObject("WbemScripting.SWbemLocator");
    var s = obj.ConnectServer(".");
    var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
    var e = new Enumerator (properties);

 
    var output;
    output='<table border="0" cellPadding="5px" cellSpacing="1px" bgColor="#CCCCCC">';
    output=output + '<tr bgColor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';
    while(!e.atEnd())

    {
        e.moveNext();
        var p = e.item ();
        if(!p) continue;
        output=output + '<tr bgColor="#FFFFFF">';
        output=output + '<td>' + p.Caption; + '</td>';
        output=output + '<td>' + p.MACAddress + '</td>';
        output=output + '</tr>';
    }

    output=output + '</table>';
    document.getElementById("box").innerHTML=output;
}
</script>


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, Javascript , ,

PHP for Microsoft AJAX Library

20. February 2010

Author: Aamir Hasan

PHP updated  support for Microsoft AJAX Library. It works with the RTM bits, and fixes several of the issues reported in my earlier samples. , so that you can join in and contribute to the project.The Microsoft AJAX Library is a standalone collection of the standards-based JavaScript classes included in Microsoft ASP.NET AJAX. it simply supports exposing PHP classes as AJAX-enabled web services, just as in ASP.NET applications. In fact, the generated proxies are identical to what you get from ASP.NET


Test  Example

<?php

require_once '../../dist/MSAjaxService.php';

class TestService extends MSAjaxService
{
function SayHello($name)
{
return 'Welcome, ' . $name . '!';
}
}

$h = new HelloService();
$h->ProcessRequest();

?>
 
 Index.html
<html>
<head>
<title>Hello, World!</title>
<script type="text/javascript" src="../../MicrosoftAjaxLibrary/MicrosoftAjax.js"></script>
<script type="text/javascript" src="HelloService.php/js"></script>
</head>
<body>
Name: <input id="name" type="text" /> <input type="button" value="Say Hello" onclick="button_click(); return false;" />
<br />
Response from server: <span id="response"></span>
</body>
<script type="text/javascript">
function button_click() {
TestService.SayHello($get('name').value, function (result) { $get('response').innerHTML = result; });
}
</script>
</html>
 


 
Oh, and if you are interest in PHP, you might want to check out the FastCGI  work on IIS to make PHP rock on IIS7. 

Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

Ajax, Javascript, PHP , , ,

Display row# in gridview

19. February 2010

  How to display row#  in gridview, DataGrid,Repeater or DataListGridView is one of most widely used controls and we can do lots of things with the control.

 In ASP.Net 2.0, Datagrid is replaced by GridView control while Datagrid control is still supported. There are large numbers of tasks that can be done through grid view control.

<%@ Page Language="C#"%>
<script runat="server">

protectedvoid Page_Load(object sender, EventArgs e)
{
gridView1.DataSource =newstring[] { "Aamir Hasan", "www.studentacad.com", "aspxtutorial.com" };
gridView1.DataBind();

}
</script>
<html>
<head runat="server">
<title>GridView Row number's</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:GridView runat="server" ID="gridView1">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

 Links

GridViewRow

 


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net ,



User Name: Guest

Your Ip: 38.107.191.91
Time: