Python 如何在基于django的项目中设置Microsoft身份验证
我正在django项目中使用django microsoft auth。我跟着 现在,我可以通过Microsoft帐户(地址:)登录,但我不知道如何添加显示“使用Microsoft登录”的视图,以及如何将该视图链接到Microsoft身份验证页面。 如果有人能告诉我怎么做就好了。 你们可以看到这幅画。此处会自动添加用于登录的Microsoft按钮。如何在主页上设置这样的按钮 我找到了一种方便的Microsoft身份验证方法。我用了 微软图形。Microsoft Graph的文档编写得很好。你可以参考这个。如果您只对验证部分感兴趣,则可以忽略日历部分 首先,您应该浏览给定的教程,然后您可以轻松理解下面给出的代码 在给定的教程中,它们使用会话对用户进行身份验证。我发现Django身份验证更方便,所以我只是按照下面给出的方法编辑了回调和注销函数 这里我只编写回调和注销函数 我的问题如何解决:现在我可以简单地更改URL.py文件中的登录URL。如果我想设置一个带有登录页面的按钮,我可以简单地使用一个锚元素来引用登录URLPython 如何在基于django的项目中设置Microsoft身份验证,python,django,microsoft-graph-api,django-authentication,azure-authentication,Python,Django,Microsoft Graph Api,Django Authentication,Azure Authentication,我正在django项目中使用django microsoft auth。我跟着 现在,我可以通过Microsoft帐户(地址:)登录,但我不知道如何添加显示“使用Microsoft登录”的视图,以及如何将该视图链接到Microsoft身份验证页面。 如果有人能告诉我怎么做就好了。 你们可以看到这幅画。此处会自动添加用于登录的Microsoft按钮。如何在主页上设置这样的按钮 我找到了一种方便的Microsoft身份验证方法。我用了 微软图形。Microsoft Graph的文档编写得很好。你可以
def callback(request):
# Get the state saved in session
expected_state = request.session.pop('auth_state', '')
# Make the token request
token = get_token_from_code(request.get_full_path(), expected_state)
# Get the user's profile
user = get_user(token)
# Get user info
# user attribute like displayName,surname,mail etc. are defined by the
# institute incase you are using single-tenant. You can get these
# attribute by exploring Microsoft graph-explorer.
username = user['displayName']
password = user['surname']
email = user['mail']
try:
# if use already exist
user = User.objects.get(username=username)
except User.DoesNotExist:
# if user does not exist then create a new user
user = User.objects.create_user(username,email,password)
user.save()
user = authenticate(username=username,password=password)
if user is not None:
login(request,user)
messages.success(request,"Success: You were successfully logged in.")
return redirect('home')
return redirect('home')
def sign_out(request):
logout(request)
messages.success(request, "Successfully Logged Out")
return redirect('home')
我找到了一种方便的Microsoft身份验证方法。我用了
微软图形。Microsoft Graph的文档编写得很好。你可以参考这个。如果您只对验证部分感兴趣,则可以忽略日历部分
首先,您应该浏览给定的教程,然后您可以轻松理解下面给出的代码
在给定的教程中,它们使用会话对用户进行身份验证。我发现Django身份验证更方便,所以我只是按照下面给出的方法编辑了回调和注销函数
这里我只编写回调和注销函数
我的问题如何解决:现在我可以简单地更改URL.py文件中的登录URL。如果我想设置一个带有登录页面的按钮,我可以简单地使用一个锚元素来引用登录URL
def callback(request):
# Get the state saved in session
expected_state = request.session.pop('auth_state', '')
# Make the token request
token = get_token_from_code(request.get_full_path(), expected_state)
# Get the user's profile
user = get_user(token)
# Get user info
# user attribute like displayName,surname,mail etc. are defined by the
# institute incase you are using single-tenant. You can get these
# attribute by exploring Microsoft graph-explorer.
username = user['displayName']
password = user['surname']
email = user['mail']
try:
# if use already exist
user = User.objects.get(username=username)
except User.DoesNotExist:
# if user does not exist then create a new user
user = User.objects.create_user(username,email,password)
user.save()
user = authenticate(username=username,password=password)
if user is not None:
login(request,user)
messages.success(request,"Success: You were successfully logged in.")
return redirect('home')
return redirect('home')
def sign_out(request):
logout(request)
messages.success(request, "Successfully Logged Out")
return redirect('home')
我也在想同样的事情。我一直在尝试将管理页面上的代码调整到我的应用程序主页上,但没有成功。我找到了解决方案。我会很快回答这个问题。我在页面上有链接,但登录不起作用。我得到
提供的状态变量无效
。如果我查看管理页面上生成的URL,并将其与我的URL进行比较,则状态确实不同。但是我使用的代码与从{{microsoft\u authorization\u URL}获取URL的代码相同。我觉得我错过了一件小事。我期待着看到你的解决方案,我也在想同样的事情。我一直在尝试将管理页面上的代码调整到我的应用程序主页上,但没有成功。我找到了解决方案。我会很快回答这个问题。我在页面上有链接,但登录不起作用。我得到提供的状态变量无效
。如果我查看管理页面上生成的URL,并将其与我的URL进行比较,则状态确实不同。但是我使用的代码与从{{microsoft\u authorization\u URL}获取URL的代码相同。我觉得我错过了一件小事。我期待看到您的解决方案,谢谢,但我正在寻找使用django microsoft auth的解决方案谢谢,但我正在寻找使用django microsoft auth的解决方案