Tuesday, November 30, 2004

WindowsIdentity, WindowsPrinciple and Role based security

Here is a small code snippet I wrote to show how to use WindowsIdentity, WindowsPrinciple objects and Role based security.


// Tells the clr which principal is in use
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);


// Gets the identity of the current user
WindowsIdentity wi = WindowsIdentity.GetCurrent();


// prints the name of the current windows user
Console.WriteLine(wi.Name);


// prints the type of the authentication
Console.WriteLine(wi.AuthenticationType);


// make an object of WindowsPrinciple
WindowsPrincipal prin = new WindowsPrincipal(wi);


// prints the name of the current windows user
Console.WriteLine(prin.Identity.Name);

// checks if the user is an Administrator return false if not else return true
// This is where we check for the role based security
Console.WriteLine(prin.IsInRole(WindowsBuiltInRole.Administrator));



Sunday, November 28, 2004

Sending credentials to WebService

Sometimes you need to send your windows credentials to the webservice. A simple way can be to use Network credentials provided by Microsoft.net framework.

The credentials are made on the webservice proxy.

MyWebService.Service1 proxy = new MyWebService.Service1();
proxy.Credentials = new NetworkCredential("Administrator","adminPassword");

Now if you have windows integrated security turned on ( It can be turned on from IIS ) it will only allow those users who have the windows login credentials and other users will get the 401 Access denied error.

Saturday, November 27, 2004

Reading Xml what to choose ??

When reading Xml files you can choose XmlReader or XmlDocument. Each has a different purpose. XmlReader is a forward only non cached way of reading Xml documents. It does not remember that it passes through.

On the other hand using XmlDocument gives you advantage of jumping from one node to the other. Once an xml file is loaded into XmlDocument object , it knows the structure of the xml document and waits for your commands.

Friday, November 26, 2004

Cannot create offline cache (error while creating asp.net project)

After sitting infront of computer for 2 hours and trying to solve this crazy error which was generated each time I try to create asp.net application.

"Unable to create the offline cache in c:\inetpub\Username\wwwroot"

It really is annoying and the this is a bug in Vs.net which will be fixed in Vs.net 2005. I solved this problem by moving my directory outside the wwwroot. So I create the project in C:\inetput and it works.

Some people also solve this problem by installing Visual Studio.net 2003 from scratch.

Sunday, November 21, 2004

Converting ArrayList to the Class Array

Hi,

Sometimes we have data in the ArrayList and we need to convert it to our class type array.
ArrayList provides a method of ToArray that converts an ArrayList to the type of our class array.

here is what we can do:

return (Person[]) aList.ToArray(typeof(Person));

Here aList is the ArrayList.


Friday, November 19, 2004

Java Client connection to .net Web Service

I have just uploaded an article about "How to make a Java client that connects to an .net Web Service".

You can view my article on my website under articles link.

www.azamsharp.cjb.net


Security Attributes in Microsoft.net

You can use windows credentials on any method you like by simple placing a Security Attribute on that method. In the code sample below I just used the user coscmasm which belongs to the LAB563 Domain. So, if you log in to your windows using coscmasm only than you can use the getName method.


using System;
using System.Security.Permissions;
using System.Security.Principal;

public class Foo
{
[PrincipalPermissionAttribute(SecurityAction.Demand, Name=@"LAB563\coscmasm")]
public void getName()
{
Console.WriteLine("This is coscmasm");
}
}
class App
{
static void Main()
{
AppDomain.CurrentDomain.SetPrincipalPolicy(
PrincipalPolicy.WindowsPrincipal);
try
{
Foo f = new Foo();
f.getName();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}