zoukankan      html  css  js  c++  java
  • 通过Dapr实现一个简单的基于.net的微服务电商系统(九)——一步一步教你如何撸Dapr之OAuth2授权百度版

      在上一篇文章中案例使用了github的oauht2.0授权,实际上使用哪个平台并不局限,只要平台实现了oauth2.0标准都可以接入。本节我们聊聊如何集成百度oauth2.0。

      本来想集成微信/QQ/微博的,结果发现不是需要企业资质就是要个人认证,就百度开放平台不需要,就用百度来演示吧。

      首先我们需要注册并登录百度开发者平台,同时创建一个应用,获取它的API Key和Secret Key

      进入应用详情后在点击左下角的安全设置,配置我们的鉴权域名【http://oauth.dapreshop.com:30882】到授权回调页并禁用

        接着修改我们的component文件,录入刚才我们获取的API Key和Secret Key到clientid和clientsecret一栏,并修改scopes、authURL、redirectURL如下所示,修改完毕后记得重新apply一下

    apiVersion: dapr.io/v1alpha1
    kind: Component
    metadata:
      name: baiduauth
      namespace: dapreshop
    spec:
      type: middleware.http.oauth2
      version: v1
      metadata:
      - name: clientId
        value: ""
      - name: clientSecret
        value: ""
      - name: scopes
        value: "basic"
      - name: authURL
        value: "http://openapi.baidu.com/oauth/2.0/authorize"
      - name: tokenURL
        value: "https://openapi.baidu.com/oauth/2.0/token"
      - name: redirectURL
        value: "http://oauth.dapreshop.com:30882"
      - name: authHeaderName
        value: "myauth"

      4、修改一下获取用户信息的代码(这里是演示所以百度返回的openid我取前n位做登录名):

    var model = new Model() { login = "" };
                if (HttpContextExt.Current.Headers.Any(x => x.Key.ToLower().Equals("myauth")))
                {
                    var requestUri = new Uri($"https://openapi.baidu.com/rest/2.0/passport/users/getLoggedInUser?access_token={HttpContextExt.Current.Headers.FirstOrDefault(x => x.Key.ToLower().Equals("myauth")).Value.Replace("Bearer ", "")}");
                    var result = await httpClientFactory.CreateClient().GetAsync(requestUri);
                    if (result.IsSuccessStatusCode)
                    {
                        var content = await result.Content.ReadAsStringAsync();
                        baidumodel obj = JsonSerializer.Deserialize<baidumodel>(content);
                        HttpContextExt.Current.Response.Cookies.Append("githubuser", JsonSerializer.Serialize(new Model() { login = obj.openid.Substring(0,8), name = obj.uname, avatar_url = $"http://tb.himg.baidu.com/sys/portraitn/item/{obj.portrait}" }),
                            new Microsoft.AspNetCore.Http.CookieOptions() { Domain = "dapreshop.com" });
                        HttpContextExt.Current.Response.Redirect("http://admin.dapreshop.com:30882");
                    }
                }
                return model;

        重新启动整个demo,这时候再次点击图标,我们会跳转至百度的授权页

      回跳后重新初始化就能看到我们取到了百度授权的用户信息

  • 相关阅读:
    hi.baidu.com 百度流量统计
    Autofac is designed to track and dispose of resources for you.
    IIS Manager could not load type for module provider 'SharedConfig' that is declared in administration.config
    How to create and manage configuration backups in Internet Information Services 7.0
    定制swagger的UI
    NSwag在asp.net web api中的使用,基于Global.asax
    NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project
    JS变量对象详解
    JS执行上下文(执行环境)详细图解
    JS内存空间详细图解
  • 原文地址:https://www.cnblogs.com/gmmy/p/14741768.html
Copyright © 2011-2022 走看看