向用户显示Windows身份验证

时间:2019-01-16 15:02:06

标签: c# angular windows-authentication

我正在尝试显示Windows用户的名称,但是在角度展开模式下出现未经授权的401错误

这是我的控制器代码

[Authorize]
    [Route("api/authorization")]
    public class WinAuthController : Controller
    {
        IEmployeeService service;
        public WinAuthController(IEmployeeService service)
        {
            this.service = service;
        }

        [HttpGet]
        [Route("getuser")]
        public async Task<Employee> GetUser()
        {
            Debug.Write($"AuthenticationType: {User.Identity.AuthenticationType}");
            Debug.Write($"IsAuthenticated: {User.Identity.IsAuthenticated}");
            Debug.Write($"Nme: {User.Identity.Name}");

                var userName = User.Identity.Name.Split("\\")[1];
                var task = await service.GetEmployeeByUserName(userName);

                return await Task.FromResult(task);
        }
    }

当我调用该方法时,这就是我的ob的外观: enter image description here 所以我被认证了 但在我的客户端上,身份验证不起作用

这是服务

@Injectable()
export class UserService {
    private _authUrl = 'api/authorization';
    constructor(private http: HttpClient) {
    }

    getUser(): Observable<User> {
        return this.http.get(this._authUrl + '/getuser')
            .map(response => response)
            .catch(this.handleError);
    }

这是我的组成部分

export class UserComponent implements OnInit {
  user: User = new User();

    constructor(private userService: UserService) { }

    ngOnInit() {
        this.getUser();
    }

    getUser() {
        this.userService.getUser()
            .subscribe(user => {
                if (user === null) {
                    console.log('user = null');
                } else {
                    console.log('user != null');
                    this.user.userId = user.userId;
                    this.user.lastName = user.lastName;
                    this.user.firstName = user.firstName;
                    console.log(this.user);
                }
        });
    }

这是我的错误消息:

消息:“ http://localhost:4200/api/authorization/getuser的Http错误响应:401未经授权”

我现在该怎么办?

0 个答案:

没有答案
相关问题