【问题标题】:Integrate ActiveDirectory Javascript code into React将 ActiveDirectory Javascript 代码集成到 React
【发布时间】:2018-10-10 17:06:20
【问题描述】:

我有这个 Javascript 代码来处理 ActiveDirectory 身份验证。 我需要创建一个使用此代码的 React 组件,在 React 中实现此目的的最佳方法是什么?

var config = { url: 'ldap://compandomain.com:389',
           baseDN: 'dc=domainname,dc=com',
           username: 'user',
           password: 'pass' };

var ad = new ActiveDirectory(config);
var username = 'john.smith@domain.com';
var password = 'password';

ad.authenticate(username, password, function(err, auth) {
  if (err) {
    console.log('ERROR: '+JSON.stringify(err));
    return;
  }

  if (auth) {
    console.log('Authenticated!');
  }
  else {
    console.log('Authentication failed!');
  }
});

React 组件如下所示:

export default class ActiveDirectory extends React.Component {

  ..
  ......
  .........

  render() {
    return <div ..../>;
  }
}

【问题讨论】:

  • 我有一个类似的问题,我想在 react 应用程序上将登录的 windows 用户名传递到后端进行身份验证,您能否提供一些详细信息?提前致谢!

标签: javascript reactjs react-native active-directory react-router


【解决方案1】:

您应该能够在 componentDidMount 生命周期方法中处理此身份验证。它应该看起来像这样。

import React from 'react';
import ActiveDirectory from 'activedirectory';

export default class ActiveDirectoryComponent extends React.Component {
  state = {
    authResponse: undefined
  };

  componentDidMount() {
    var config = {
      url: 'ldap://compandomain.com:389',
      baseDN: 'dc=domainname,dc=com',
      username: 'user',
      password: 'pass'
    };

    var ad = new ActiveDirectory(config);
    var username = 'john.smith@domain.com';
    var password = 'password';

    ad.authenticate(username, password, function (err, auth) {
      if (err) {
        this.setState({ authResponse: { error: JSON.stringify(err) } });
        return;
      }

      if (auth) {
        this.setState({ authResponse: auth });
      } else {
        console.log('Authentication failed!');
        this.setState({ authResponse: { authFailed: true } });
      }
    });
  }

  render() {
    if (!this.state.authResponse) {
      return <div>Authenticating....</div>;
    }
    if (this.state.authResponse.error) {
      return <div>{this.state.authResponse.error}</div>
    }
    if (this.state.authResponse.authFailed) {
      return <div>Authentication Failed</div>
    }
    return <div>.....</div>
  }
}

【讨论】:

  • 感谢乔伊!这似乎是要走的路。我现在看到一个不同的问题,当我导入“ActiveDirectoryComponent”时,我的反应应用程序页面变为空白。这可能是因为我正在使用 webpack 打包我的应用程序但不确定。无论如何,非常感谢您的快速响应!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 2020-06-03
  • 2021-10-19
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多