2006-08-14 16:16:57 +02:00
|
|
|
using System;
|
|
|
|
using System.Text;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace Novell.Casa.Client.Auth
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Summary description for Class1.
|
|
|
|
/// </summary>
|
|
|
|
public class Authtoken
|
|
|
|
{
|
|
|
|
private const string AUTH_LIBRARY = "C:\\Program Files\\Novell\\CASA\\lib\\authtoken";
|
2006-08-18 19:41:08 +02:00
|
|
|
private const int BUFFER_OVERFLOW = 6;
|
2006-08-14 16:16:57 +02:00
|
|
|
|
|
|
|
[DllImport(AUTH_LIBRARY, CharSet=CharSet.None) ]
|
|
|
|
public static extern int ObtainAuthToken
|
|
|
|
(
|
|
|
|
[In] byte[] baService,
|
|
|
|
[In] byte[] baHost,
|
2006-08-18 19:41:08 +02:00
|
|
|
[In, Out] byte[] baToken,
|
|
|
|
[In, Out] ref int iTokenLength
|
2006-08-14 16:16:57 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Authtoken()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static byte[] ObtainAuthToken(string sService, string sHost)
|
|
|
|
{
|
|
|
|
int rcode = 0;
|
|
|
|
byte[] baService = null;
|
2006-08-18 19:41:08 +02:00
|
|
|
byte[] baHost = null;
|
|
|
|
int bufferSize = 0;
|
2006-08-14 16:16:57 +02:00
|
|
|
|
|
|
|
byte[] baToken = new byte[bufferSize];
|
|
|
|
|
|
|
|
// convert service to ascii byte array
|
|
|
|
if (sService != null)
|
|
|
|
{
|
|
|
|
baService = Encoding.ASCII.GetBytes(sService);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid parameter");
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert host to ascii byte array
|
|
|
|
if (sHost != null)
|
|
|
|
{
|
|
|
|
baHost = Encoding.ASCII.GetBytes(sHost);
|
|
|
|
}
|
2006-08-18 19:41:08 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid parameter");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// call with buffersize of 0. This way we determine the exact size.
|
2006-08-14 16:16:57 +02:00
|
|
|
try
|
|
|
|
{
|
2006-08-18 19:41:08 +02:00
|
|
|
rcode = ObtainAuthToken(baService, baHost, baToken, ref bufferSize);
|
|
|
|
|
|
|
|
int test = (rcode & BUFFER_OVERFLOW);
|
|
|
|
if (test == BUFFER_OVERFLOW)
|
|
|
|
{
|
|
|
|
// now allocate the proper size
|
|
|
|
baToken = new byte[bufferSize];
|
|
|
|
rcode = ObtainAuthToken(baService, baHost, baToken, ref bufferSize);
|
|
|
|
}
|
2006-08-14 16:16:57 +02:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
LogMessage(e.ToString());
|
2006-08-18 19:41:08 +02:00
|
|
|
return null;
|
2006-08-14 16:16:57 +02:00
|
|
|
}
|
2006-08-18 19:41:08 +02:00
|
|
|
|
2006-08-14 16:16:57 +02:00
|
|
|
|
|
|
|
if (rcode != 0)
|
2006-08-18 19:41:08 +02:00
|
|
|
{
|
2006-08-14 16:16:57 +02:00
|
|
|
throw new Exception(rcode.ToString());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-18 19:41:08 +02:00
|
|
|
return baToken;
|
|
|
|
}
|
2006-08-14 16:16:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void LogMessage(string sMessage)
|
|
|
|
{
|
|
|
|
System.Diagnostics.Trace.WriteLine("(C#)AuthToken: " + sMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|