【发布时间】:2019-04-09 09:37:01
【问题描述】:
我创建 .Net Core API 并配置 Windows 身份验证。
在我的角度应用程序中,我必须在每个请求中输入此选项withCredentials : true。我做了一个放置请求,但它返回给我:
我也尝试过发布请求,但它不能只获得请求工作。
auth.service.ts :
updateUser(id,lastlogin,ac,lastlogoff,picture){
return this.http.put(`${this.config.catchApiUrl()}User/`+ id , {
id : id ,
picture : picture,
lastLogin : lastlogin ,
lastLogoff : lastlogoff ,
ac: ac
},{
headers : this.header,
withCredentials : true
})
}
auth.component.ts:
constructor(
private authService : AuthenticationService
) { }
loginToApplication(username :string){
this.authService.updateUser(e["id"],lastLogin,e["ac"],e["lastLogoff"],e["picture"]).subscribe(
console.log("enter"))
}
.Net Core API 更新用户控制器:
[AllowAnonymous] // Even if I do this it doesn't work
[HttpPut("{id}")]
public async Task<ActionResult<User>> UpdateUser(int id, User user)
{
try {
var ldapPath = Environment.GetEnvironmentVariable("path");
var ldapUsername = Environment.GetEnvironmentVariable("user");
var ldapPassword = Environment.GetEnvironmentVariable("psw");
DirectoryEntry Ldap = new DirectoryEntry(ldapPath, ldapUsername, ldapPassword);
DirectorySearcher searcher = new DirectorySearcher(Ldap);
var users = await _context.Users.Include(t => t.Access).FirstOrDefaultAsync(t => t.Id == id);
if (users == null)
{
return StatusCode(204);
}
if(users.Ac== 1 && user.Ac!= 1)
{
return BadRequest("You can't change an administrator profile");
}
searcher.Filter = "(SAMAccountName=" + users.Login.ToLower() + ")";
SearchResult result = searcher.FindOne();
if (result == null)
{
return NoContent();
}
DirectoryEntry DirEntry = result.GetDirectoryEntry();
user.Lastname = DirEntry.Properties["sn"].Value.ToString();
user.Fullname = DirEntry.Properties["cn"].Value.ToString();
user.Firstname = DirEntry.Properties["givenname"].Value.ToString();
user.Login = DirEntry.Properties["samaccountname"].Value.ToString().ToLower();
user.Email = DirEntry.Properties["mail"].Value == null ? "No mail" : DirEntry.Properties["mail"].Value.ToString(); ;
_context.Entry(users).CurrentValues.SetValues(user);
_context.SaveChanges();
return users;
}
catch (Exception ex)
{
return StatusCode(204,ex.Message);
}
}
即使我在控制器顶部添加[AllowAnonymous] 也不起作用。
更新:
我的项目中有 Cors,startup.cs
在ConfigureServices:
services.AddCors(options =>
{
options.AddPolicy("AnyOrigin", builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
在Configure:
app.UseCors("AnyOrigin");
此外,如果我想在邮递员上进行测试,我会遇到此错误(也在获取请求中,但在我的项目中获取请求工作)
401 - 未经授权:由于凭据无效,访问被拒绝。 您无权使用您提供的凭据查看此目录或页面。
当我在我的 api 网站上(我使用 swagger)时,我有这个:
你的连接不是私人的
【问题讨论】:
-
您是否在
Startup.cs的Configure方法中将.AllowCredentials()包含在您的.net 核心api 的CORS 策略中? -
@Arul 是的,我这样做:services.AddCors(options => { options.AddPolicy("AnyOrigin", builder => { builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials (); }); });
-
您的 API 部署在哪里?既然您说本地主机上的 api 工作正常,那么您的 api 和您从中访问 api 的客户端是否在同一个域上?
-
@Arul 我的 api 部署在 IIS 上,并且在同一个域中
-
你解决过这个问题吗?我有同样的情况...尝试了几天来解决它...
标签: angular windows-authentication put asp.net-core-2.1