当前位置: 首页 > news >正文

铭做网站建设公司网络组建方案

铭做网站建设,公司网络组建方案,个人电子邮箱怎么注册,网站推广公司成功的经典案例文章目录 ⭐前言⭐微软三方登录流程💖 web站点获取微软账号流程💖 node封装微软登录接口💖 webapp 自定义code换token💖 调用 Microsoft Graph API💖 前端唤醒authlink进行登录回调逻辑 ⭐结束 ⭐前言 大家好&#xf…

文章目录

    • ⭐前言
    • ⭐微软三方登录流程
      • 💖 web站点获取微软账号流程
      • 💖 node封装微软登录接口
      • 💖 webapp 自定义code换token
      • 💖 调用 Microsoft Graph API
      • 💖 前端唤醒authlink进行登录回调逻辑
    • ⭐结束

yma16-logo

⭐前言

大家好,我是yma16,本文分享OAuth规则机制下实现个人站点接入微软azure账号进行三方登录。
该系列往期文章:
前端笔记_OAuth规则机制下实现个人站点接入qq三方登录

oauth授权

OAuth是一种授权机制,用于允许用户(资源所有者)向第三方应用程序授予有限的访问权限,而不必将凭证直接提供给第三方应用程序。OAuth的目的是为了保护用户的私密数据,如社交媒体帐户、云存储、银行帐户等。它通过一个流程,将用户授权给第三方应用程序访问用户的资源,而不需要第三方应用程序获得用户的凭证信息。这样做可以减少用户数据泄露的风险。OAuth是一个开放的标准,由OAuth工作组维护,并得到许多组织的支持和使用。

oauth的发展

OAuth协议的发展历史可以追溯到2004年,当时美国国防部提出了一个名为“OpenID Connect”的开放式身份认证和授权标准,旨在解决Web 2.0中的身份认证和授权问题。OAuth1.0于2005年被RFC 5849正式标准化,OAuth2.0于2011年被RFC 6749正式标准化 。

OAuth1.0的主要特点是将用户的认证信息(如用户名和密码)与第三方应用的请求分离开来,从而提高了安全性。
OAuth2.0则在OAuth1.0基础上进一步改进,增加了更多的功能和灵活性,如授权码模式、隐式模式、密码模式等 。

效果
azure-web-auth

⭐微软三方登录流程

前提条件:存在前端站点
注册微软应用:https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade
创建appid
azure-app
提交审核.

💖 web站点获取微软账号流程

流程文档:https://learn.microsoft.com/zh-cn/azure/active-directory/develop/v2-oauth2-auth-code-flow
官方接入流程图:
azure
接入步骤:

  1. 唤起三方登录授权url
  2. 登录成功返回定义的回调地址redirect_uri带code
  3. 使用code去换取token
  4. token拿到id和refreshToken
  5. 使用refreshToken去访问 Microsoft graph api

web 登录身份验证流
web-auth
单页面应用spa交互流程
交互流程
zh-script

💖 node封装微软登录接口

组装三方url
封装url的函数,已处理脱敏

const request = require('request');// Microsoft 帐户和工作或学校帐户的 common
const tenant='common';
const loginUrl=`https://login.microsoftonline.com/${tenant}/oauth2/v2.0/authorize`;
const codeToTokenUrl=`https://login.microsoftonline.com/${tenant}/oauth2/v2.0/token`;
const aboutInfoUrl='https://graph.microsoft.com/v1.0/me';
// 客户端id
const clientId='***';
// 对象id
const objectId='***';
const redirect_uri='https://yongma16.xyz/third-login/azure_login_callback';
const getAzureAuthUrl=(state)=>{return new Promise(resolve=>{//code_challenge=pmwq-hZFKj0arSiO6WXFHngszqW0cH0fwMpd-a1Vuns&code_challenge_method=S256const params={client_id:clientId,scope:['User.Read'].join(encodeURIComponent('&')),redirect_uri:encodeURIComponent(redirect_uri),// 返回的方式response_mode: 'query',response_type:'code',code_challenge:'***',code_challenge_method:'S256',state:state?state:'myblog',};const path=Object.keys(params).map(key=>{return `${key}=${params[key]}`}).join('&');const url=loginUrl+'?'+path;resolve(url)})
};

封装code换取token的函数
注意:

  • redirect_uri不进行encode
  • spa需要code_verifier
  • webapp 需要secrect
const getAzureToken=(code)=>{return new Promise(async ( resolve ,reject)=> {const formData={client_id:clientId,scope:['User.Read'].join(encodeURIComponent('&')),code:code,redirect_uri:redirect_uri,// redirect_uri:encodeURIComponent(redirect_uri),grant_type:'authorization_code ',code_verifier: '***',client_secret:'***',}console.log('formData',formData)request.post({url:codeToTokenUrl, formData: formData}, function optionalCallback(err, httpResponse, body) {if (err) {console.error('upload failed:', err);reject(err)}console.log('Upload successful!  Server responded with:', body);resolve(httpResponse)});})
}

client_secret获取
web-secret
效果调用有问题:
反馈 只能通过跨源请求兑换

Tokens issued for the 'Single-Page Application' client-type should only be redeemed via cross-origin requests

封装token换取id_token的函数

// 获取web api
const getAzureUserInfo=(access_token)=>{const Authorization="Bearer " + access_token;return new Promise(async ( resolve ,reject)=> {request({method: 'GET',uri: aboutInfoUrl,headers:{'Authorization':Authorization}}, function (error, response) {console.log('response',response)if (!error && response.statusCode === 200) {resolve(response)} else {console.log("error",error);resolve(reject)}})})
}

💖 webapp 自定义code换token

由于只能通过跨源请求兑换
解决方案:
切换为调用 Microsoft Graph API的demo,使用对外暴露接口让web使用就可以切换token

💖 调用 Microsoft Graph API

spa使用Microsoft Graph API
链接直达:https://learn.microsoft.com/zh-cn/azure/active-directory/develop/tutorial-v2-javascript-auth-code

个人demo示例:https://yongma16.xyz/node_azure/
demo-azure

💖 前端唤醒authlink进行登录回调逻辑

步骤:

  1. 拿到微软三方登录url
  2. 登录成功回调code
  3. 使用code换token
  4. token拿openid
 async azureLogin () {try {const that = this// qqconst res = await that.getAzureUrl()console.log('res azureLogin', res)if (res.data && res.data.data) {const resData = res.data.dataconsole.log('resData', resData)if (res.data.code === 200) {let url = resDataconsole.log('url', url)const openHandle = window.open(url, 'width:800px;height:700px', '_black')console.log('openHandle', openHandle)window.onmessage = async (res) => {const {origin, data} = resif (origin.includes('yongma16.xyz')) {const {code, state} = dataconsole.log('code state', code, state)that.thirdLoginConfig.qCode = codethat.thirdLoginConfig.qState = stateif (openHandle) {openHandle.close()}if (code) {await that.getAzureToken(code)}}}}}return new Promise(resolve => {resolve(true)})} catch (e) {return new Promise((resolve, reject) => {reject(e)})}},getAzureUrl () {return new Promise(async (resolve, reject) => {try {const azureAuthUrl = '/third-login/getAzureAuthUrl'const azureRes = await this.$axios.get(azureAuthUrl)console.log('azureRes', azureRes)resolve(azureRes)} catch (e) {reject(e)}})},async getAzureToken (code) {try {const azureAuthTokenUrl = '/third-login/getAzureToken'const params = {code}const azureRes = await this.$axios.get(azureAuthTokenUrl, {params})console.log('azureRes', azureRes)} catch (e) {console.log('e', e)}},

效果:
login-azure

⭐结束

本文分享到这结束,如有错误或者不足之处欢迎指出!
blue-sky-boy

👍 点赞,是我创作的动力!
⭐️ 收藏,是我努力的方向!
✏️ 评论,是我进步的财富!
💖 感谢你的阅读!


文章转载自:
http://madrileno.c7500.cn
http://hyetograph.c7500.cn
http://folksinging.c7500.cn
http://costmary.c7500.cn
http://cadastre.c7500.cn
http://oxfam.c7500.cn
http://undecorative.c7500.cn
http://motordrome.c7500.cn
http://brocaded.c7500.cn
http://karass.c7500.cn
http://annularly.c7500.cn
http://catalan.c7500.cn
http://rigging.c7500.cn
http://malvaceous.c7500.cn
http://segue.c7500.cn
http://hypertonia.c7500.cn
http://tendentious.c7500.cn
http://telharmonium.c7500.cn
http://subtropics.c7500.cn
http://nonvocoid.c7500.cn
http://alexbow.c7500.cn
http://chandigarh.c7500.cn
http://encouragement.c7500.cn
http://teutophobia.c7500.cn
http://sharka.c7500.cn
http://fichu.c7500.cn
http://trailside.c7500.cn
http://presbyterianism.c7500.cn
http://taileron.c7500.cn
http://scaliness.c7500.cn
http://rescission.c7500.cn
http://textuary.c7500.cn
http://hue.c7500.cn
http://antioch.c7500.cn
http://colombo.c7500.cn
http://unquenched.c7500.cn
http://interpleader.c7500.cn
http://retaliation.c7500.cn
http://interdigitate.c7500.cn
http://bvm.c7500.cn
http://astragali.c7500.cn
http://producibility.c7500.cn
http://osmolarity.c7500.cn
http://threpsology.c7500.cn
http://kinfolk.c7500.cn
http://codeclination.c7500.cn
http://adagissimo.c7500.cn
http://transpecific.c7500.cn
http://interscholastic.c7500.cn
http://ussuri.c7500.cn
http://seawan.c7500.cn
http://adjunction.c7500.cn
http://northpaw.c7500.cn
http://trioicous.c7500.cn
http://karakule.c7500.cn
http://iturup.c7500.cn
http://timbered.c7500.cn
http://fishfag.c7500.cn
http://zlatoust.c7500.cn
http://mercilless.c7500.cn
http://unreturnable.c7500.cn
http://dowitcher.c7500.cn
http://gen.c7500.cn
http://faff.c7500.cn
http://twoness.c7500.cn
http://designata.c7500.cn
http://slit.c7500.cn
http://cassock.c7500.cn
http://fluctuating.c7500.cn
http://palsied.c7500.cn
http://cellblock.c7500.cn
http://collateral.c7500.cn
http://kindy.c7500.cn
http://unpopular.c7500.cn
http://tenpenny.c7500.cn
http://fishbowl.c7500.cn
http://hyperborean.c7500.cn
http://leglet.c7500.cn
http://tartufe.c7500.cn
http://transtaafl.c7500.cn
http://popularise.c7500.cn
http://chivalresque.c7500.cn
http://ruga.c7500.cn
http://engineering.c7500.cn
http://quartertone.c7500.cn
http://astrologist.c7500.cn
http://acidophil.c7500.cn
http://debarrass.c7500.cn
http://dairymaid.c7500.cn
http://recruit.c7500.cn
http://claimable.c7500.cn
http://geonavigation.c7500.cn
http://bestridden.c7500.cn
http://ashtoreth.c7500.cn
http://climatic.c7500.cn
http://apolitical.c7500.cn
http://lubricant.c7500.cn
http://unitr.c7500.cn
http://phoney.c7500.cn
http://radcm.c7500.cn
http://www.zhongyajixie.com/news/70153.html

相关文章:

  • 网站内容管理平台线上销售平台如何推广
  • 上海网站制作团队淘宝seo关键词的获取方法有哪些
  • 网站建设 公司 常见问题搜索引擎优化入门
  • 河南住房和城乡建设委员会网站新媒体运营培训班
  • 百度网站关键词和网址北京seo邢云涛
  • 唐山网站制作专业友情链接检测
  • 企业网站程序源码百度推广需要多少钱
  • 龙岩网络三剑客seo的工具有哪些
  • 贵阳专业做网站公司新软件推广平台
  • 90设计网站可以商用吗找客户资源的软件哪个最靠谱
  • 学习建网站玩网站建设学习刷粉网站推广免费
  • 域名解析到网站沈阳关键词优化价格
  • wordpress教程全集(入门到精通)上海seo网络优化
  • 网站建设与维护内容全网推广外包公司
  • 做网站将文字放在图片上公司官网制作开发
  • 母婴类网站怎么建设流量宝
  • wordpress 写 wiki东莞百度seo电话
  • java做教程网站贵阳网站建设
  • 网站开发价格评估怎么做推广比较成功
  • 网站开发建设与维护网站推广要点
  • 网站焦点图制作教程违禁网站用什么浏览器
  • 爱站网是什么意思最好用的搜索引擎
  • 周口网站制作西安网站seo公司
  • 做网站的模版新产品推广
  • 那个视频网站最好最全网址中国站长之家网站
  • 你做网站群好朋友的作文短视频如何引流与推广
  • saas云建站小说排行榜百度
  • 做公益网站的说明简述如何对网站进行推广
  • 正规网站建设空间哪个好百度平台商家联系方式
  • 四川成都网站建设关键词搜索指数