CASA/CASA-auth-token/non-java/client/csharp/Novell.Casa.Authtoken/Authtoken.cs
2006-09-20 15:19:17 +00:00

121 lines
3.0 KiB
C#

/***********************************************************************
*
* Copyright (C) 2006 Novell, Inc. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, Novell, Inc.
*
* To contact Novell about this file by physical or electronic mail,
* you may find current contact information at www.novell.com.
*
* Author: Jim Norman
*
***********************************************************************/
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";
private const int BUFFER_OVERFLOW = 6;
[DllImport(AUTH_LIBRARY, CharSet=CharSet.None) ]
public static extern int ObtainAuthToken
(
[In] byte[] baService,
[In] byte[] baHost,
[In, Out] byte[] baToken,
[In, Out] ref int iTokenLength
);
public Authtoken()
{
}
public static byte[] ObtainAuthToken(string sService, string sHost)
{
int rcode = 0;
byte[] baService = null;
byte[] baHost = null;
int bufferSize = 0;
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);
}
else
{
throw new Exception("Invalid parameter");
}
// call with buffersize of 0. This way we determine the exact size.
try
{
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);
}
}
catch (Exception e)
{
LogMessage(e.ToString());
return null;
}
if (rcode != 0)
{
throw new Exception(rcode.ToString());
}
else
{
return baToken;
}
}
private static void LogMessage(string sMessage)
{
System.Diagnostics.Trace.WriteLine("(C#)AuthToken: " + sMessage);
}
}
}