【问题标题】:Azure AD authentication with Rails and devise使用 Rails 和设计的 Azure AD 身份验证
【发布时间】:2019-05-21 16:45:59
【问题描述】:

我有一个带有 devise 配置和 mongodb 数据库的 rails 应用程序。我想配置 Microsoft azure AD 进行身份验证。当用户输入我的项目 url 并且用户未登录时,它应该重定向到 azure AD 的登录页面,并且当凭据正确时,它应该重定向回我的应用程序。我关注this 博客来实现我的要求。但它会引发随机错误。有人可以建议我怎么做吗?

【问题讨论】:

  • 问题是否解决?

标签: ruby-on-rails devise azure-active-directory omniauth devise-token-auth


【解决方案1】:
class Integrations::Crm::MsDynamics

extend ActiveSupport::Concern

#to instantiate a new dynamics link directory_id/tenant_id,client_id/application_id,secret,username,password and resource link eg. https://maropost.crm3.dynamics.com
def initialize(tenant_id,client_id,client_secret,username,password,resource)
  @tenant_id=tenant_id
  @client_id=client_id
  @client_secret=client_secret
  @username=username
  @password=password
  @resource=resource
end

def get_token
  uri = URI.parse("https://login.microsoftonline.com/#{@tenant_id}/oauth2/token")
  request = Net::HTTP::Post.new(uri)
  request.content_type = "application/x-www-form-urlencoded"
  request["Cache-Control"] = "no-cache"
  request.set_form_data(
    "client_id" => "#{@client_id}",
    "resource" => "#{@resource}",
    "username" => "#{@username}",
    "password" => "#{@password}",
    "grant_type" => "password",
    "client_secret" => "#{@client_secret}",
  )
  req_options = {
    use_ssl: uri.scheme == "https",
  }
  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  return response
 end


 def get_access_token(code)
  uri = URI.parse("https://login.microsoftonline.com/#{@tenant_id}/oauth2/token")
  request = Net::HTTP::Post.new(uri)
  request.content_type = "application/x-www-form-urlencoded"
  request["Cache-Control"] = "no-cache"
  request.set_form_data(
    "client_id" => "#{@client_id}",
    "client_secret" => "#{@client_secret}",
    "code" => "#{code}",
    "grant_type" => "authorization_code",
    "redirect_uri" => "#{SSL_APP_SITE}/dynamic_crms_callbacks/dynamic_authorization_code",
    "resource" => "#{@resource}",
  )

  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
   return response
 end

 def ms_dynamics(response)
  obj = JSON.parse(response.body)
  client = MSDynamics.new({
      hostname: "#{@resource}",
      access_token: obj["access_token"],
      refresh_token: obj["refresh_token"],
      client_id: "#{@client_id}",
      client_secret: "#{@client_secret}"
  })
  return client
 end

end

请参考此代码,它将解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-11
    • 2016-07-08
    • 1970-01-01
    • 2017-12-15
    • 2019-10-15
    • 1970-01-01
    • 2016-04-07
    • 2019-06-25
    相关资源
    最近更新 更多