Monday, October 24, 2016

How to transfer multiple values from one Page to other using Session as Class?





Step1: Page A

StudentsClass object= new StudentsClass()
        {
            Name= txtName.Text,
            Class = txtClass.Text,
       };

Session["Stu"]=object;

Step2: Page B

StudentsClass obj= (StudentsClass)Session["Stu"];

string name = obj.txtName;
string class  = obj.txtClass;



Tuesday, October 4, 2016

How to Query REST API ?



> domain\username:password must be authorize to query to the REST API.
> For POST HttpRequest is content type and use charset=utf-8 to convert.
> For GET HttpRequest is Accept


POST:


Uri address = new Uri(@"https://xx.xx.xx.xx:xxxx/abc/config/anything/");

HttpWebRequest request = WebRequest.CreateDefault(address) as HttpWebRequest;
request.Method = "POST";
request.Headers["Authorization"] = "Basic " + 
Convert.ToBase64String(Encoding.Default.GetBytes(@"domain\username:password"));
request.ContentType = "application/vnd.com.cisco.ise.identity.guestuser.2.0+xml; charset=utf-8";
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });

//Inserting data 

string data="xml data";


byte[] bytes = Encoding.UTF8.GetBytes(data);

request.ContentLength = bytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
  // Send the data.
   requestStream.Write(bytes, 0, bytes.Length);
  requestStream.Close();
 }


Get By Name:


string name="";


Uri address = new Uri(@"https://xx.xx.xx.xx:xxxx/abc/config/anything/name/" +name +" ");


HttpWebRequest request = WebRequest.CreateDefault(address) as HttpWebRequest;

request.Method = "GET";
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(@"domain\username:password"));
request.Accept = "application/vnd.com.cisco.ise.identity.guestuser.2.0+xml";
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
       
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string s = reader.ReadToEnd();

}

Get All:


Uri address = new Uri(@"https://xx.xx.xx.xx:xxxx/abc/config/anything/ ");


HttpWebRequest request = WebRequest.CreateDefault(address) as HttpWebRequest;

request.Method = "GET";
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(@"domain\username:password"));
request.Accept = "application/vnd.com.cisco.ise.identity.guestuser.2.0+xml";
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
       
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string s = reader.ReadToEnd();

}

            


Monday, October 3, 2016

How to create a web service ?



Step1: Create the Web Service

Open Visual Studio create new web site. File > New >Website

Add an item as Web Service to the project. Webservice.asmx

Now create some web methods 


 [WebMethod]
 public string GetbyId(string name){}


if the response is in Custom Created Class then return that class object as below
 
[WebMethod]
[System.Xml.Serialization.XmlInclude(typeof(Students))]
 public string GetAll(string name){}


Public Class Student
{
public string name{get; set;}
public string rollnumber{get; set;}
}

Step 2: Publishing the Web Service

To publish the web service the procedure is same as publishing the website because as we created the Web Service under Web Site Project.




Secure you Asp .NET by Web.config & Global.ascx?

Add to Global.ascx protected void Application_BeginRequest(object sender,EventArgs e)     {         //to remove x frame         Resp...