Hướng dẫn authentication in python - xác thực trong python

Chuyển đến nội dung chính

Trình duyệt này không còn được hỗ trợ nữa.

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

End-user authentication with Azure Data Lake Storage Gen1 using Python

  • Bài viết
  • 04/29/2022
  • 2 phút để đọc

Trong bài viết này

  • Using Java
  • Using .NET SDK
  • Using Python
  • Using REST API

In this article, you learn about how to use the Python SDK to do end-user authentication with Azure Data Lake Storage Gen1. End-user authentication can further be split into two categories:

  • End-user authentication without multi-factor authentication
  • End-user authentication with multi-factor authentication

Both these options are discussed in this article. For service-to-service authentication with Data Lake Storage Gen1 using Python, see Service-to-service authentication with Data Lake Storage Gen1 using Python.

Prerequisites

  • Python. You can download Python from here. This article uses Python 3.6.2.. You can download Python from here. This article uses Python 3.6.2.

  • An Azure subscription. See Get Azure free trial.. See Get Azure free trial.

  • Create an Azure Active Directory "Native" Application. You must have completed the steps in End-user authentication with Data Lake Storage Gen1 using Azure Active Directory.. You must have completed the steps in End-user authentication with Data Lake Storage Gen1 using Azure Active Directory.

Install the modules

To work with Data Lake Storage Gen1 using Python, you need to install three modules.

  • The azure-mgmt-resource module, which includes Azure modules for Active Directory, etc.
  • The azure-mgmt-datalake-store module, which includes the Azure Data Lake Storage Gen1 account management operations. For more information on this module, see Azure Data Lake Storage Gen1 Management module reference.
  • Mô -đun azure-datalake-store, bao gồm các hoạt động của hệ thống tập tin Gen1 của Azure Data Lake. Để biết thêm thông tin về mô-đun này, hãy xem tham chiếu mô-đun tập tin của Azure-Datalake-Store.

Sử dụng các lệnh sau để cài đặt các mô -đun.

pip install azure-mgmt-resource
pip install azure-mgmt-datalake-store
pip install azure-datalake-store

Tạo một ứng dụng Python mới

  1. Trong IDE của sự lựa chọn của bạn, hãy tạo một ứng dụng Python mới, ví dụ, mysample.py.

  2. Thêm đoạn trích sau để nhập các mô -đun cần thiết

    ## Use this for Azure AD authentication
    from msrestazure.azure_active_directory import AADTokenCredentials
    
    ## Required for Azure Data Lake Storage Gen1 account management
    from azure.mgmt.datalake.store import DataLakeStoreAccountManagementClient
    from azure.mgmt.datalake.store.models import DataLakeStoreAccount
    
    ## Required for Azure Data Lake Storage Gen1 filesystem management
    from azure.datalake.store import core, lib, multithread
    
     # Common Azure imports
     import adal
     from azure.mgmt.resource.resources import ResourceManagementClient
     from azure.mgmt.resource.resources.models import ResourceGroup
    
     ## Use these as needed for your application
     import logging, pprint, uuid, time
    
  3. Lưu các thay đổi vào mysample.py.

Xác thực người dùng cuối với xác thực đa yếu tố

Cho quản lý tài khoản

Sử dụng đoạn trích sau đây để xác thực với Azure AD cho các hoạt động quản lý tài khoản trên tài khoản lưu trữ dữ liệu Gen1. Đoạn trích sau đây có thể được sử dụng để xác thực ứng dụng của bạn bằng xác thực đa yếu tố. Cung cấp các giá trị dưới đây cho một ứng dụng gốc Azure AD hiện có.native application.

authority_host_url = "https://login.microsoftonline.com"
tenant = "FILL-IN-HERE"
authority_url = authority_host_url + '/' + tenant
client_id = 'FILL-IN-HERE'
redirect = 'urn:ietf:wg:oauth:2.0:oob'
RESOURCE = 'https://management.core.windows.net/'

context = adal.AuthenticationContext(authority_url)
code = context.acquire_user_code(RESOURCE, client_id)
print(code['message'])
mgmt_token = context.acquire_token_with_device_code(RESOURCE, code, client_id)
armCreds = AADTokenCredentials(mgmt_token, client_id, resource = RESOURCE)

Cho các hoạt động hệ thống tập tin

Sử dụng điều này để xác thực với Azure AD cho các hoạt động hệ thống tập tin trên tài khoản lưu trữ dữ liệu Gen1. Đoạn trích sau đây có thể được sử dụng để xác thực ứng dụng của bạn bằng xác thực đa yếu tố. Cung cấp các giá trị dưới đây cho một ứng dụng gốc Azure AD hiện có.native application.

adlCreds = lib.auth(tenant_id='FILL-IN-HERE', resource = 'https://datalake.azure.net/')

Xác thực người dùng cuối mà không có xác thực đa yếu tố

Điều này không được chấp nhận. Để biết thêm thông tin, hãy xem Xác thực Azure bằng SDK Python.

Bước tiếp theo

Trong bài viết này, bạn đã học cách sử dụng xác thực người dùng cuối để xác thực với Azure Data Lake Storage Gen1 bằng Python. Bây giờ bạn có thể xem các bài viết sau đây nói về cách sử dụng Python để làm việc với Azure Data Lake Storage Gen1.

  • Hoạt động quản lý tài khoản trên Data Lake Storage Gen1 bằng cách sử dụng Python
  • Hoạt động dữ liệu trên Data Lake Storage Gen1 bằng cách sử dụng Python

Phản HồI

Gửi và xem ý kiến ​​ph