We use this code to get the email of the currently logged in user:
var currentUsersEmail = UserPrincipal.Current.EmailAddress;
This works fine when on the network. However, we have laptop users who run our code remotely and in a disconnected state. The above code causes this exception when in this state:
System.DirectoryServices.AccountManagement.PrincipalServerDownException
Message: "The server could not be contacted."
Does anyone know a way to retrive the current user's email that will work both when connected / on the network and when disconnected?
UPDATE: In case you are wondering why we want to do this. Our system has a bunch of methods that send alerts via email. When our developers are running automated tests, we want the emails to be sent to the current logged in user (i.e. the developer) so they can verify the format.
You could rewrite your application so that it uses UserPrincipal.Current.EmailAddress the first time, and perhaps whenever it is online, and then saves that information in the user's AppData folder or registry for reference when working offline. It would require running the application at least one online before working offline, but I don't see how else you could do it without having to ask the user or use a config file.
There is simply no way the System.DirectoryServices.AccountManagement namespace will work offline. It's like asking how can a database driven application work without access to the database.
Related
** Edit - Based on further questoins **
I'm writing a c# application, that uses ConfidentialClientApplication. I have set up the application in azure and can connect and get a token.
The software will in fact, reside on a server, it will look something like this:
A backend process will query the mailbox and get either a) A list of messages or b) the first message available
It will download hte message and strip any attachments for further processing
Once the processing is complete, it will need to move the mail to a folder, so that it is not processed again
I want to be able to pass the application, the mailbox name and the password to connect to it, a bit like POP3/IMAP. So far, I can get a token, but anything beyond that, just barks that it doesnt have access, quite rightly.
So how do I take this one step further, and either:
Grant the application access to a specific mailbox using azure portal
or
Configure the application to read the mailbox from a settings file and connect to it?
Many thanks!
Tirm
I have a desktop application made in C#. The application connects to a Solaris server through SSH and performs certain jobs there. The login to the app is based on user's credentials used for logging into the Solaris server. When the user inputs his Solaris credentials, it hits the server and checks if the cred is good.
The problem is, the app runs the job in background worker. For most users the app runs fine, but for a particular user, the myworker.RunWorkerAsync() doesn't work, which means the control never passes to do_work.
I have tried debugging it from a different machine but still the same result. So, its not system specific issue, but I am at loss to understand, why for everybody the background worker is created properly and runs but only for the particular user it doesn't.
If it works for everyone but one or two people, then it's those people's accounts you need to check. Either they are non-functional / disabled, or not configured the same as the others.
Try diffing a working user and non-working user's profile to see what the differences are and then try reconcile them.
Another route is to use a failing account and try all the exact steps that the app does manually via you favorite ssh tool.
Also, always make sure you are trapping for errors correctly: Unhandled exceptions in BackgroundWorker
Hi I have a strange problem and am unable to find out the root cause. We build and deployed our application using Redemption things were working fine but suddenly the installation on UAT server has stopped working. But the same is working in production(Thank God).
Our application is of console based and written using .NET 3.5 its not totally down, it is working but when we try to run the same application via scheduler task it didn't work, but when we double click the exe file it works properly, successfully connect to Exchange reads and download the files as per our business logic but not executing when we try to run under scheduler task.
below is small error info might be helpful for you..
2012-06-15 16:05:06,724 EmailProcess.Program - message = Error in
IMAPISession::OpenMsgStore: MAPI_E_FAILONEPROVIDER ulVersion: 0 Error:
Microsoft Exchange is not available. Either there are network
problems or the Exchange computer is down for maintenance. Component:
Microsoft Exchange Information Store ulLowLevelError: 2147746069
ulContext: 1318
Please suggest
Edit 1: More info Windows 2008 Server, a user named SpecialTasksUser is configured on the server to login and has admin rights. Outlook is installed and profile is configured for different user name OutlookSpecialUser, when we login using SpecialTasksUser user and launch the outlook it loads and shows the inbox and private folders...so far so good.
We created the scheduled task and configured to run under user name SpecialTasksUser because this user has admin rights and this is where it gives error. On the other hand, when we go to installation directory and double click the *.exe file the application works and redemption successuflly reads the email and does what it is suppose to do...please help
I hope this information would be helpful .....
Most likely you are using a wrong parent identity of your process - scheduler runs under its own account by default. Make sure you specify the right Windows account.
I created a windows service that's basically a file watcher that wont run unless a user is logged into the machine its on.
The service is running on a Windows Server 2003 machine. It is designed to listen for excel files in a folder. When there is a excel file, it starts to send some information to a web service. When it's done, it copies the processed file to a archive folder.
Can anyone help me?
Best regards
Baris
Run it as a user that has rights to log on as service on the machine. Make sure this user has an access to the directory you watch.
What i always do in a scenario like that is give the user logon as batch job and logon as a service rights. 1 of these 2 will most likely fix your problem.
You can configure this in the local group policy editor as described here
Be aware though that if your computer is in a domain it is possible that the group policy gets pushed to the server every 15 mins so you might have to talk to a system admin to get things sorted.
When you actually only want to run when someone is logged in, do not use a service but an autostart application in that case.
If you have to be a service because of account privileges, the service may detect the current logins itself, but you may combine a service with a client (autostart) application that connects to the service. That way, you can also show tray incos, status informations and enable the user to control your service using the client application.
Using Win7 and higher, services themselves (running in session 0) can no longer display UI interactions on the user's desktop.
Keep in mind that there may be multiple users logged in on current operating systems...
I would like to send a message to an Active Directory user so when he/she loggs in next time they see the message though a popup window. Is it possible to do it in any way beside using the usual netsend?
This is usually a mess when trying to push out a message to a user. If you want a message to pop up, I would suggest that you create a small application with a pull mechanism. When the user logs in, the application should run to determine if they have a message waiting (messages could be in a central database or even file location). If they do, the application could pop up the message. In this way, the application would also bypass most of your firewall/network segment issues. You would only need to open communication to a central location for the data rather than diagnosing network issues for every client.
Not only would this method allow you to do exactly what you are asking for, it would allow you to expand your application to do even more. For example, you could require the user to acknowledge the message in a more formal way than just hitting OK (maybe make them check a box first saying "I read and understand the message"). You could also capture the time when the user saw the message (they might not log in for hours after you send the message). It could also be used when the user is currently logged in and you want to send them a message (the app could poll the server).
While there are downsides to this method (creating an app, installing it everywhere, managing it, etc.) I think the benefits would outweigh the downsides if you really need to have a notification system like you are specifying.
On Windows 2003 you've got an old fashion way of doing that.
In the scriptPath attribut of the AD user nod, you put the name of a script or exe file for example username.cmd.
On your DC, you've got a folder C:\WINDOWS\SYSVOL\sysvol\DOMAIN.NAME\SCRIPTS shared with the name NETLOGON. you just create the file username.cmd in this folder. the program username.cmd is going to be played on every logon of the user. In this file you can put a popup message.
It's still working on Windows Server 2008 R2.