CASA/CASA-auth-token/non-java/client/csharp/Novell.Casa.Authtoken/Authtoken.cs
Jim Norman f145a46ffe
2006-08-14 14:16:57 +00:00

101 lines
2.0 KiB
C#

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";
[DllImport(AUTH_LIBRARY, CharSet=CharSet.None) ]
public static extern int ObtainAuthToken
(
[In] byte[] baService,
[In] byte[] baHost,
IntPtr pToken,
[In, Out] ref int iTokenLength
);
public Authtoken()
{
//
// TODO: Add constructor logic here
//
}
public static byte[] ObtainAuthToken(string sService, string sHost)
{
int rcode = 0;
byte[] baService = null;
byte[] baHost = null;
int bufferSize = 4096;
IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize);
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);
}
LogMessage("Calling Native API->ObtainAuthToken");
LogMessage("Buffer size is "+ bufferSize);
try
{
rcode = ObtainAuthToken(baService, baHost, pBuffer, ref bufferSize);
}
catch (Exception e)
{
LogMessage(e.ToString());
}
LogMessage("ObtainAuthToken returned " + rcode);
if (rcode != 0)
{
Marshal.FreeHGlobal(pBuffer);
throw new Exception(rcode.ToString());
}
else
{
// marshal the IntPtr into a byte array
String sTemp = Marshal.PtrToStringAnsi(pBuffer, bufferSize);
Marshal.FreeHGlobal(pBuffer);
return (Encoding.ASCII.GetBytes(sTemp));
}
return null;
}
private static void LogMessage(string sMessage)
{
System.Diagnostics.Trace.WriteLine("(C#)AuthToken: " + sMessage);
}
}
}