gpt4 book ai didi

Django登录而不进行身份验证

转载 作者:行者123 更新时间:2023-12-04 02:44:30 25 4
gpt4 key购买 nike

我有一个 Django 应用程序,其中有 2 个用例,我希望用户无需密码即可登录。

  • 用户注册并在电子邮件中获得激活链接。
  • 用户重置密码并获取链接以更改电子邮件中的密码表单。

  • 这些链接包括我验证的一次性使用 key ,然后我想在不使用凭据的情况下登录用户。
    # This raises an exception unless
    # I call user.authenticate 1st.
    auth.login(request, user)

    我如何实现这一目标?

    最佳答案

    您可以编写自己的身份验证后端来处理您的两个用例。请参阅有关编写和使用自定义身份验证后端的文档:
    http://docs.djangoproject.com/en/1.2/topics/auth/#other-authentication-sources
    编辑:
    关于编写自己的身份验证后端可能有多困难,似乎存在一些误解。从文档:

    An authentication backend is a classthat implements two methods:get_user(user_id) andauthenticate(**credentials).


    这是正确的。它是任何实现两个函数的类,它们都返回 User对象。

    The get_user method takes a user_id --which could be a username, database IDor whatever -- and returns a Userobject.

    ...authenticate should check thecredentials it gets, and it shouldreturn a User object that matchesthose credentials, if the credentialsare valid. If they're not valid, itshould return None.


    OP 已经声明链接包含他验证的一次性 key (并且可能与他希望登录的用户相关联)。换句话说,他已经为后端编写了业务逻辑,他只需要将其转换为合适的类即可。
    自定义身份验证后端可以在 Django 1.2 中做很多很棒的事情,比如对象级权限,但它们不必那么复杂。此外,它们堆叠在一起,因此您可以将基于 token 的身份验证与默认模型后端或 OpenID 或 Facebook 混合。但最终,身份验证后端只是具有两种方法的类,我不知道您如何称其为矫枉过正。

    关于Django登录而不进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3807777/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com