ara*_*333 6 asp.net-core-webapi
我的Web Core API中的此代码确认我是经过身份验证的用户,但我没有获取用户名,而是获取应用程序池的名称.我该如何解决?
var testa = User.Identity.GetType();
NLogger.Info($"testa = {testa}");
var testd = User.Identity.IsAuthenticated;
NLogger.Info($"testd = {testd}");
var loggedInUser = User.Identity.Name;
NLogger.Info($"loggedInUser = {loggedInUser}");
Run Code Online (Sandbox Code Playgroud)
在我的日志文件中,我得到;
testa = System.Security.Principal.WindowsIdentity
testd = True
loggedInUser = IIS APPPOOL\SIR.WEBUI
Run Code Online (Sandbox Code Playgroud)
我使用控制器的[Authorize]标签并禁用匿名身份验证.
好吧,我从Postman调用方法,它工作正常,LoggedInUser是正确的.但是当我从我的代码中调用时,我得到了上面显示的不正确的loggedInUser.我用来从我的客户端应用程序调用web api的代码是;
public static async Task<IEnumerable<ContractDto>> GetAllForUser()
{
using (var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }))
{
client.BaseAddress = new Uri(AppSettings.ServerPathApi);
var path = GetPath("getallforuser");
var response = await client.GetAsync(path);
response.EnsureSuccessStatusCode();
var stringResult = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<IEnumerable<ContractDto>>(stringResult);
}
}
Run Code Online (Sandbox Code Playgroud)
在IIS中,我将应用程序池类型设置为所有各种选项; applicationpoolidentity,networkservice,localservice,localsystem,每次都测试应用程序.我究竟错过了什么?
尝试这个
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
string username = System.Web.HttpContext.Current.User.Identity.Name;
}
Run Code Online (Sandbox Code Playgroud)
或者你可以尝试
var user = (System.Security.Principal.WindowsIdentity)HttpContext.Current.User.Identity;
var userName = user.Impersonate();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |