问题
I already tried link from stackoverflow
I have a silverlight client and wcf service. The application has 3 authentication modes
- Active Directory
- Windows Pass Through
- Proprietary - I don't have a problem with this one obviously. (I really don't know what is the difference between active directory and Window Pass Through. I just use the same code for both, except for windows pass through I get the current user and for AD the app prompts for username password)
.
private string GetCurrentUser()
{
try
{
string result = WindowsIdentity.GetCurrent().Name;
WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
result = wp.Identity.Name;
if (result.Contains(@"\"))
{
int position = result.LastIndexOf(@"\") + 1;
result = result.Substring(position);
}
return result;
}
catch (Exception ex)
{
return "";
}
}
Both WindowsIdentity and WindowsPrincipal returns 'DefaultAppPool' or whatever the AppPool the current thread runs. Even Environment.UserName returns the same.
When I turn on <identity impersonate ="true"/>
in web.config the silverlight client fails to connect to wcf. It gets a 'Not Found' error. So, I need to keep <identity impersonate ="false"/>
All I need is the current logged on user, I didn't know that it's this difficult.
回答1:
I changed the identity on the Application Pool to my own user account and it worked.
- Open IIS Console
- Select Application Pools.
- Select the AppPool (in my case it was DefaultAppPool).
- On the right pane click Advanced Settings.
- There are different categories of settings like General, CPU, Process Model.
- Under Process Model -> Identity click the right side input box, a button shows up, click it.
- It opens a dialog box with 2 radio buttons (Built-in account and Custom account).
- Select custom account and hit Set.
- Set Credentials dialog box opens.
- Enter your credentials and hit okay. You may have to enter [domain][user name]
- Hit Ok to all the dialog boxes to close everything.
- Now test your app, WindowsIdentity.GetCurrent().Name should return the username associated with the Application Pool.
来源:https://stackoverflow.com/questions/11131228/how-to-get-current-windows-user-in-wcf