Get Current User within Lookups API

Discussions related to order tracking development with the ProForm module.

Moderator: Phil Barton

czentman
Posts: 157
Joined: Tue Dec 02, 2008 12:02 pm

Re: Get Current User within Lookups API

Post by czentman »

Basicly, I am using the Lookups API. I am doing a custom form (with an editor window) to be able to add/edit/remove records from a specific lookup table. I would like to capture the person logged into the system to see who made those edits/inserts into the phonebook.

When I do the the selectserver sps - I do use the New, with the credentials (see below) as Admin, passw0rd, but if I don't use that (meaning I comment out the 2 lines of credentials and new sps, and uncomment the code you gave me), then I get the following security error. "Security Warning. Object reference not set to an instance of an object."



Imports SoftPro.Select.Client.SelectServer
Imports SoftPro.Select.Client.SelectServerObject
Imports SoftPro.Select.Server
Imports SoftPro.OrderTracking.Common
Imports SoftPro.Select.Server.Common
Imports System
Imports System.Net
Imports SoftPro.Select.Client.Constants
Imports System.Windows.Forms
Imports SoftPro.OrderTracking.Client
Imports SoftPro.Select.Shell
Imports MaAccessLib

Public Class frmClientPhonebook
Public table As DataTable, dt As DataTable
Public table2 As System.Data.DataTable
'connect using the select server object
Public credentials As New NetworkCredential("admin", "Passw0rd", SoftPro.Select.Client.Constants.SelectDomain)
Public sps As New SoftPro.Select.Client.SelectServer("http://localhost/SelectServer", credentials)


'Public sps As SoftPro.Select.Client.SelectServer
'Public sps As SoftPro.Select.Client.SelectServer = TryCast(GetService(GetType(SoftPro.Select.Client.SelectServer)), SoftPro.Select.Client.SelectServer)
Public lookups As Lookups = sps.GetService(Of Lookups)()

Public spec As New LookupQuerySpec()
Public spec2 As New LookupQuerySpec()



'Public lookups As Lookups = sps.GetService(Of Lookups)()
Public lookups2 As Lookups = sps.GetService(Of Lookups)()
Public Event FormClosing As FormClosingEventHandler
Private oHost As MaAccessLib.MaHost
Private oPeople As MaAccessLib.People
Private oDetail As MaAccessLib.Detail
Private oNote As MaAccessLib.Notes
Private sDbLoginHandle As Object

Private Sub frmClientPhonebook_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' sps = TryCast(GetService(GetType(SoftPro.Select.Client.SelectServer)), SoftPro.Select.Client.SelectServer)

cboLookupType.Items.Add("Abstractors") 'searchers TX01Name only need for title
cboLookupType.Items.Add("Attorney")
cboLookupType.Items.Add("Lender") 'Lender table in Proform Lookups
cboLookupType.Items.Add("Mortgage Broker")
cboLookupType.Items.Add("Settlement Agent") '
cboLookupType.Items.Add("Surveyor")
cboLookupType.Items.Add("Other")

Dim username As String = sps.AuthenticatedUserName

End Sub
Public Sub GridViewRefresh(ByVal TableName As String, ByVal FilterText As String, ByVal FilterField As String)

sps.EnsureAuthenticated()
If FilterField <> "" And FilterText <> "" Then
spec.Filter = "[" & FilterField & "] = '" & FilterText & "'"
Else
spec.Filter = "[Lookup Code] = '*'"
End If

table = lookups.GetTable(TableName, spec)
'table.AcceptChanges()
table.Columns("Lookup Code").SetOrdinal(0)
table.Columns("Name").SetOrdinal(1)
table.Columns("Payee Name").SetOrdinal(2)
table.Columns("Email").SetOrdinal(3)
table.Columns("Marketing Rep").SetOrdinal(4)
table.Columns("Phone").SetOrdinal(5)
table.Columns("Fax").SetOrdinal(6)
table.Columns("Address (line 1)").SetOrdinal(7)
table.Columns("Address (line 2)").SetOrdinal(8)
table.Columns("City").SetOrdinal(9)
table.Columns("State").SetOrdinal(10)
table.Columns("Zip").SetOrdinal(11)

DataGridView1.DataSource = table
DataGridView1.AutoResizeColumns()


End Sub
Mark McKenna

Re: Get Current User within Lookups API

Post by Mark McKenna »

Given the nature of the exception message, would you be able to provide the exact line in your source that triggers it? Just step through it using a debugger.

Thank you.
czentman
Posts: 157
Joined: Tue Dec 02, 2008 12:02 pm

Re: Get Current User within Lookups API

Post by czentman »

lookups = sps.GetService(Of Lookups)()

This is where it gives the error, when trying to get the lookups service from the sps
Mark McKenna

Re: Get Current User within Lookups API

Post by Mark McKenna »

What version of Select are you currently running?

Also, does it work if you use the runtime service provider to get the authenticated user name, but then use a new SelectServer instance to get to the Lookups service? Kind of like this:

Code: Select all

// use the SelectServer associated with the runtime service provider to get the authenticated username of the person logged in
Select.Client.SelectServer sps = GetService( typeof( Select.Client.SelectServer ) ) as Select.Client.SelectServer;
string username = sps.AuthenticatedUserName;

// use a new SelectServer to access the Lookups functionality
System.Net.ICredentials credentials = new System.Net.NetworkCredential( "admin", "Passw0rd", SoftPro.Select.Client.Constants.SelectDomain );
sps = new SoftPro.Select.Client.SelectServer( "http://localhost/SelectServer", credentials );
SoftPro.OrderTracking.Client.Lookups lookups = sps.GetService<SoftPro.OrderTracking.Client.Lookups>( );
Mark McKenna

Re: Get Current User within Lookups API

Post by Mark McKenna »

Wait a minute.

Are you popping this form up from the editor window? That code was missing from your snippet. If you are, then it is very likely that the GetService call is being resolved against the Form itself (since Form is a Component and Component has a GetService method too), in which case it won't find a registered service of type SelectServer. The exception you're seeing may be nothing more than your sps reference is null and so the sps.GetService() call fails.

To show a Form or any of its derivatives from your code you will want to use the SoftPro.Select.Shell.IDialogProvider interface from the editor window. This ensures that your Form is sited properly and has access to our service provider.

Code: Select all

// from your editor window
SoftPro.Select.Shell.IDialogProvider dialogProvider = GetService<SoftPro.Select.Shell.IDialogProvider>( );
using ( MyPasswordForm form = new MyPasswordForm( ) )
{
    DialogResult result = dialogProvider.ShowDialog( form );
    ...
}
czentman
Posts: 157
Joined: Tue Dec 02, 2008 12:02 pm

Re: Get Current User within Lookups API

Post by czentman »

No. There is no form popping up from the editor window. My form is the editor window. I don't want a dialog form. I don't think the Idialog is what I want. I don't see why this is so complex. I just want to know if I can get the userid and pwd for who is logged in to the system without opening an order.
jbright

Re: Get Current User within Lookups API

Post by jbright »

czentman wrote:I just want to know if I can get the userid and pwd for who is logged in to the system without opening an order.
You can get a list of all users currently logged in to a server by using the Licensing class. Passwords are not available, because they are one-way encrypted before they are stored in the database.

Code: Select all

SoftPro.Select.Client.SelectServer selectServer = GetService<SoftPro.Select.Client.SelectServer>();

SoftPro.Select.Client.Licensing licensing = selectServer.GetService<SoftPro.Select.Client.Licensing>();

var users = (from l in licensing.EnumUserLeases() select l.UserName).Distinct();
czentman
Posts: 157
Joined: Tue Dec 02, 2008 12:02 pm

Re: Get Current User within Lookups API

Post by czentman »

I mean, who is logged in via this application. I have a customization - it has a new editor window. Upon clicking this, I want to know who it is that is logged in via this app open now. Not all the users that are currently logged in. Who am I? How am I logged in, similar to a username object in microsoft that we all the time in microsoft apps -

Dim objNet
Set objNet = CreateObject("WScript.NetWork")
UserName = objNet.UserName
Set objNet = Nothing

createdby = UserName

Maybe I should just use this from microsoft, but I'd rather know from select (in case the windows username is different from the one on select).
roteague
Posts: 292
Joined: Thu Sep 25, 2008 4:49 pm
Location: Honolulu, Hawaii

Re: Get Current User within Lookups API

Post by roteague »

I think the SP uses network credentials, so you should be able to get the via:

Code: Select all

using System.Security.Principal;


WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());          
MessageBox.Show(wp.Identity.Name);
Robert
czentman
Posts: 157
Joined: Tue Dec 02, 2008 12:02 pm

Re: Get Current User within Lookups API

Post by czentman »

Thanks! This is what I wanted. I figured that it would be part of the API package but this is great. Thanks.
Post Reply