问题描述
我已经尝试过来自stackoverflow的链接
我有一个 Silverlight 客户端和 wcf 服务.应用有3种认证模式
I have a silverlight client and wcf service. The application has 3 authentication modes
- 活动目录
- Windows 直通
- 专有 - 我显然对这个没有问题.(我真的不知道 Active Directory 和 Window Pass Through 有什么区别.我只是对两者使用相同的代码,除了 windows pass through 我获取当前用户和 AD 应用程序提示输入用户名密码)立>
.
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 "";
}
}
WindowsIdentity 和 WindowsPrincipal 都返回DefaultAppPool"或当前线程运行的任何 AppPool.甚至 Environment.UserName 返回相同.
Both WindowsIdentity and WindowsPrincipal returns 'DefaultAppPool' or whatever the AppPool the current thread runs. Even Environment.UserName returns the same.
当我在 web.config 中打开 <identity impersonate ="true"/>
时,silverlight 客户端无法连接到 wcf.它收到未找到"错误.所以,我需要保持
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.
推荐答案
我将应用程序池上的身份更改为我自己的用户帐户,并且成功了.
I changed the identity on the Application Pool to my own user account and it worked.
- 打开 IIS 控制台
- 选择应用程序池.
- 选择 AppPool(在我的例子中是 DefaultAppPool).
- 在右侧窗格中,单击高级设置.
- 有不同类别的设置,例如常规、CPU、进程模型.
- 在 Process Model -> Identity 下点击右侧输入框,出现一个按钮,点击它.
- 它会打开一个带有 2 个单选按钮(内置帐户和自定义帐户)的对话框.
- 选择自定义帐户并点击设置.
- 设置凭据对话框打开.
- 输入您的凭据并点击确定.您可能需要输入 [域][用户名]
- 对所有对话框点击确定"以关闭所有内容.
- 现在测试您的应用,WindowsIdentity.GetCurrent().Name 应返回与应用程序池关联的用户名.
这篇关于如何在 wcf 中获取当前 Windows 用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!