26 Nov 2006
If you ever wanted to add a simple username/password authentication to your web service, but ended up with a whole lot of this ?
[WebMethod]
public string HelloWorld(string userName,string password)
Well then, here is a much cleaner way. You can use SOAP headers to pass extra information to a web service. This method uses SOAP headers to pass the user credentials to the web service.
The web service.
We need an object to hold the user credentials. For this example a simple class with username and password properties would suffice. The class should derive from the SoapHeader class.
public class Authentication:SoapHeader
{
private string _userName;
private string _password;
public string Password
{
get { return _password; }
set { _password = value; }
}
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
}
In the web service class, declare a public field (or property) of the Authentication type.
WebService(Namespace = “[http://tempuri.org/”)]])
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1,Name=”MyWebService”)]
public class MyWebService : System.Web.Services.WebService {
public Authentication ServiceCredentials;
In the next step, set up the web method to accept a SOAP header, of the type Authentication, and assign the value to the ServiceCredentials member.
[WebMethod]
[SoapDocumentMethod(Binding="MyWebService")]
[SoapHeader("ServiceCredentials") ]
public string HelloWorld() {
if (ServiceCredentials.UserName == "test" &&
ServiceCredentials.Password == "world") {
return "Hello World";
}
else
{
return "Invalid authentication";
}
}
At the client.
- Add the web service reference as usual. Instantiate a new object of the type MyWebService.
- In addition instantiate a new object of the type Authentication and assign the username and password properties.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<Authentication xmlns="http://tempuri.org/">
<Password>string</Password>
<UserName>string</UserName>
</Authentication>
</soap:Header>
<soap:Body>
<HelloWorld xmlns="http://tempuri.org/" />
</soap:Body>
</soap:Envelope>
</font>
25 Nov 2006
The Object Data Source (ODS) helps you expose your business objects, to provide data to databound controls such as the GridView, DetailsView and many other data bound controls.
Compared to the other data sources like the DataTable and DataSet, the ODS does require some work to achieve the same functionality. The main advantage of the ODS is that it allows you to maintain your layers, without polluting your presentation layer with data code.
To start off, drag and drop the ODS control to a web form. Set the TypeName property to the business object you want to work with. For example, if we have a BO named Employees in the TimeTracker namespace, this will be TimeTracker.Employee.
Selecting
A common scenario is to display the a list of records. Say we want to display the list of all employees in a GridView. To retrieve the records, the Employee object needs a GetEmployees static method that will retrieve all the employees from the DB. Set the SelectMethod property of the ODS to GetEmployees. Now bind the ODS to the GridView just like any other data source.
At runtime the ODS uses reflection to find the GetEmployees method and invokes it.
Paging and filtering
Filtering Employee objects is as easy as adding a new method to your Employee object. If we want to select Employees by department, the Employee object should have a static method
GetEmployees(string department)
In the properties window of the ODS, add the department parameter to the SelectParameters collection. You can set a default value to it, and this is where things get real easy. You can bind the parameter to a control on your web form ( like a DropDownList) , a Session variable and even a query string parameter. Adding filtering functionality is simple as that. You can do the same programmatically by adding Parameters to the SelectParameters collection. Be sure to clear the collection before you add a parameter( or check if the parameter exists).
Paging is where working with the ODS gets a bit complicated. To enable paging ( in conjunction with the GridView) set the EnablePaging property to true. The Employee object too, needs to have methods to support paging.
When paging is enabled, the ODS calls the GetEmployee method with two extra parameters. by default the parameters are startRowIndex and maximumRows. These can be changed by setting the StartRowIndexParameterName and MaximumRowsParameterName properties.
Now we need a GetEmployees method with the signature like
GetEmployees(int startRowIndex,int maxiumRows)
To support filtering it will have to be GetEmployees(string department ,int startRowIndex,int maxiumRows)
The maximumRows parameter, will have the value of how many rows to display per page. This will have the value of the PageSize property of the GridView. The startRowIndex will contain the the index of the current page. This can be the starting row index of the current page of records. See here for more on how to pass these on to an SQL query.
In addition to this the ODS needs another method that is set by the SelectCountMethod property. This is invoked to find out the total number of rows available. So we need another static method in our Employee object. GetEmployeesCount() .If the total number of rows is 100, and you have a page size of 20. The GetEmployees count method should return 100, so that the ODS can tell the GridView, how many pager links to display. I usually return this as an out parameter from the stored procedure that retrieves the paged results.
Inserting, Editing and Deleting
Inserting, editing and deleting is pretty easy. All you will again is to have the appropriate static methods in the Employee object.
AddEmployee(Employee e)
UpdateEmployee(Employee e)
DeleteEmployee(Emplyee e).
Set the InsertMethod , UpdateMethod and DeleteMethod properties accordingly. All these method can be configured with parameters like the SelectMethod. However, setting the DataObjectTypeName property to the Employee object will reduce a lot of the hassle by passing Employee objects to the Add,Update and DeleteMethods.
07 Oct 2006
http://www.moo.com/
Had mine delivered on Monday and they are pretty cool 🙂 Mini calling cards just the size of a chewing gum. Each card can have a unique photo, in matt print. The printing is high quality.
10 cards free for Flickr Pro users.
07 Oct 2006
A few issues to keep in mind when converting a VS 2005 web project to a Web Application project.
http://webproject.scottgu.com/CSharp/Migration2/Mi…
ASP .Net automatically generates a strongly typed Profile class when using Profile Personalization (System.Web.Profiles). This does not happen when using WAP (Web application projects).
There is a free utility to generate the Profile class from the web.config
http://www.gotdotnet.com/workspaces/workspace.aspx…
The default SqlProfilesProvider works by serializing all the profile properties into one column. However, as with all providers this can be changed.
http://www.asp.net/downloads/teamprojects/default…. is a download to a Table profile provider and stored procedure profile provider. These work out of the box, to a profiles table of your own choosing.
23 Sep 2006
I was looking into ASP.Net Webparts last week. Here are a few helpful resources.
Sahil Malik introduces Webparts by conducing a walkthrough of creating a qucik and not so dirty CMS (content management system) link
A good introduction into writing custom editors for web parts by the same author.
A MSDN article giving an overview of the whole framework.