gpt4 book ai didi

angular - 如何解决 401 Angular 未经授权的问题?

转载 作者:行者123 更新时间:2023-12-03 14:56:01 27 4
gpt4 key购买 nike

我创建了 .Net Core API 并配置了 Windows 身份验证。
在我的 Angular 应用程序中,我必须在每个请求中添加此选项 withCredentials : true .我做了一个放置请求,但它返回给我:

401 (Unauthorized) 401 unauthorized 401 network



我也尝试发布请求,但它不起作用,只能获取请求工作。

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 更新用户 Controller :
[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]在 Controller 的顶部它不起作用。

更新:
我的项目中有 Cors, 启动.cs
ConfigureServices :
services.AddCors(options =>
{
options.AddPolicy("AnyOrigin", builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});

Configure :
app.UseCors("AnyOrigin");

此外,如果我想对 postman 进行测试,我会遇到此错误(也在获取请求中,但在我的项目中获取请求工作)

401 - Unauthorized: Access is denied due to invalid credentials. You do not have permission to view this directory or page using the credentials that you supplied.



当我在我的 api 网站上时(我使用 swagger),我有这个:

Your connection is not private

最佳答案

首先要做的事情是:您可以使用 Postman 之类的工具发出授权请求吗?如果是,则查看正确的请求 header (以及可能的有效负载),并找出 Angular 发送的请求 header 中缺少什么。

此外,您在控制台中有一个 CORS 错误(除其他外)。一个快速 (&dirty) 修复方法是启用 CORS 用户端(Firefox 有一个插件 CORS Anywhere,Chrome 有一些命令行选项),然后再次检查。

试试那个。

关于angular - 如何解决 401 Angular 未经授权的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55589622/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com