Scenario:
We wanted to test access to our service for kerberose.
Solution:
Small console application to test if i can reach the resource. Make sure you are testing a secured resource , and the username used to make the call has SPN created.
Code:
using System;Follow me on Twitter
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
namespace Training {
class Program {
public static void Main(string[] args) {
Console.WriteLine("Kerberose Test Application");
string sUrl = @"http://localhost/getDoc.aspx?id=66";
try
{
//Display what Authentication modules are registered
DisplayRegisteredModules();
// Unregister the standard Basic, NTLM and Negotiate and Digest modules, leaving only Kerberos
// AuthenticationManager.Unregister("Basic");
// AuthenticationManager.Unregister("NTLM");
// AuthenticationManager.Unregister("Negotiate");
// AuthenticationManager.Unregister("Digest");
AuthenticationManager.Unregister("Kerberos");
//Display what Authentication modules are left registered
DisplayRegisteredModules();
// Prepare the web page we will be asking for
var request = (HttpWebRequest)WebRequest.Create(sUrl);
request.UserAgent = "MOSSPH";
request.Proxy = null;
Console.WriteLine();
Console.WriteLine(string.Format("Trying to access :{0}",sUrl));
// TODO. Establish your own security context.
//request.Credentials = CredentialCache.DefaultCredentials;
request.Credentials = new NetworkCredential("username", "password", "domain");
//request.Credentials = new NetworkCredential(args[0], args[1], args[2]);
//CredentialCache wrCache = new CredentialCache();
//wrCache.Add(new Uri(sUrl), "Kerberos", new NetworkCredential("username", "password", "domain"));
//request.Credentials = wrCache;
//CredentialCache wrCache = new CredentialCache();
//wrCache.Add(new Uri(sUrl), "Negotiate", new NetworkCredential("username", "password", "domain"));
//request.Credentials = wrCache;
var response = (HttpWebResponse)request.GetResponse();
// we will read data via the response stream
Console.WriteLine(response.ContentType.ToString());
Console.WriteLine((ulong)response.ContentLength);
Console.WriteLine(response.StatusDescription.ToString());
Stream stream = response.GetResponseStream();
DisplayPageContent(stream);
// Displays all the headers present in the response received from the URI.
Console.WriteLine("\r\nThe following headers were received in the response:");
// Displays each header and it's key associated with the response.
for (int i = 0; i < response.Headers.Count; ++i)
Console.WriteLine("\nHeader Name:{0}, Value :{1}", response.Headers.Keys[i], response.Headers[i]);
Console.WriteLine("\nHeader Name:{0}, Value :{1}", response.ContentType.ToUpper(), response.ContentLength.ToString());
// Releases the resources of the response.
response.Close();
}
catch (Exception ex) {
Console.Write("Error occured:" + ex);
}
finally {
Console.Read();
}
}
private static void DisplayRegisteredModules() {
IEnumerator registeredModules = AuthenticationManager.RegisteredModules;
Console.WriteLine("\r\nThe following authentication modules are now registered with the system:");
while (registeredModules.MoveNext())
{
Console.WriteLine("\r \n Module : {0}", registeredModules.Current);
var currentAuthenticationModule = (IAuthenticationModule)registeredModules.Current;
Console.WriteLine("\t CanPreAuthenticate : {0}", currentAuthenticationModule.CanPreAuthenticate);
}
}
// The DisplayPageContent method display the content of the selected page.
private static void DisplayPageContent(Stream receiveStream) {
// Define the byte array to temporarily hold the current read bytes.
var read = new Byte[512];
// Read the first 512 bytes.
int bytes = receiveStream.Read(read, 0, 512);
Console.WriteLine("\r\nPage Content...\r\n" + Encoding.ASCII.GetString(read, 0, bytes));
}
}
}
0 comments:
Post a Comment