{"id":727,"date":"2018-05-23T18:43:20","date_gmt":"2018-05-24T01:43:20","guid":{"rendered":"http:\/\/cywang.no-ip.org\/wordpress\/?p=727"},"modified":"2018-05-23T19:05:00","modified_gmt":"2018-05-24T02:05:00","slug":"rails-authentication-with-microsoft-active-directory","status":"publish","type":"post","link":"http:\/\/cywang.no-ip.org\/wordpress\/?p=727","title":{"rendered":"Rails authentication with Microsoft Active Directory"},"content":{"rendered":"<p>Following sample code shows how Rails integrates with Microsoft Active Directory using net-ldap gem.<\/p>\n<p>Active Directory Environment: Windows Server 2012 R2<\/p>\n<p>Development Environment: CentOS 7.5<\/p>\n<p>Rails Version: 5.2.0<br \/>\n<!--more--><\/p>\n<h1>Create a Rails application<\/h1>\n<pre><code class=\"language-bash\">rails new ldap_example<\/code><\/pre>\n<p>Since I use PostgreSQL instead of sqlite3, I got following error:<\/p>\n<pre><code class=\"language-bash\">An error occurred while installing sqlite3 (1.3.13), and Bundler cannot continue.\r\nMake sure that `gem install sqlite3 -v '1.3.13'` succeeds before bundling.<\/code><\/pre>\n<p>Therefore, I use <strong>&#8211;with-pg-config<\/strong> option to use PostgreSQL.<\/p>\n<pre><code class=\"language-bash\">rails new ldap_example --skip-spring --skip-turbolinks --skip-test-unit  --with-pg-config=\/usr\/bin\/pg_config --database=postgresql<\/code><\/pre>\n<h1>Add net-ldap to the Gemfile<\/h1>\n<pre><code class=\"language-ruby\"># Net::LDAP \r\ngem 'net-ldap'<\/code><\/pre>\n<h1>Install net-ldap<\/h1>\n<pre><code class=\"language-bash\">bundle install<\/code><\/pre>\n<h1>Create a controller<\/h1>\n<pre><code class=\"language-bash\">rails generate controller\u00a0sessions<\/code><\/pre>\n<h1>Connect to your AD<\/h1>\n<pre class=\"line-numbers\"><code class=\"language-ruby\">class SessionsController &lt; ApplicationController\r\n  def login\r\n    ldap = Net::LDAP.new  :host =&gt; '10.1.33.16',\r\n          :port =&gt; '389',\r\n          :auth =&gt; {\r\n            :method =&gt; :simple,\r\n            :username =&gt; \"cywang@mydomain.com\",\r\n            :password =&gt; \"tEsTpAsSwOrD1234\"\r\n          }\r\n\r\n    if ldap.bind\r\n      render json: {'Result': 'Login Succeeded!'}, status: status\r\n    else\r\n      render json: {'Result': 'Login failed'}, status: status\r\n    end\r\n  end\r\nend<\/code><\/pre>\n<p>You can simply change the password or username to test your login method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following sample code shows how Rails integrates with Microsoft Active Directory using net-ldap gem. Active Directory Environment: Windows Server 2012 R2 Development Environment: CentOS 7.5 Rails Version: 5.2.0<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[32],"tags":[36],"class_list":["post-727","post","type-post","status-publish","format-standard","hentry","category-ruby-on-rails","tag-active-directory"],"_links":{"self":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=727"}],"version-history":[{"count":5,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/727\/revisions"}],"predecessor-version":[{"id":744,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/727\/revisions\/744"}],"wp:attachment":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=727"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}