首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用微软帐户登录Azure AD V2应用程序

使用微软帐户登录Azure AD V2应用程序
EN

Stack Overflow用户
提问于 2018-11-30 00:39:06
回答 1查看 101关注 0票数 0

我正在使用Azure AD V2应用程序进行基于Angular的应用程序身份验证。我正在使用ASP.NET网络API作为后端。

我使用身份验证码流来获取身份验证码,并使用它获取用户访问令牌和刷新令牌,如文章:https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow中所述

此流程适用于我的Azure AD帐户。当我使用outlook帐户登录时,它会为我提供代码,但当我尝试使用代码获取访问令牌时,它不起作用,并且在响应中出现错误请求错误。

以下是我在Web API中使用的代码:

代码语言:javascript
复制
        public async Task<string> GetAccessToken(string code)
        {
            string token = "", refresh_token = "";            

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            var keyValuePairs = new List<KeyValuePair<string, string>>();
            keyValuePairs.Add(new KeyValuePair<string, string>("scope", AppsConfiguration.idaScope));
            keyValuePairs.Add(new KeyValuePair<string, string>("client_id", AppsConfiguration.idaClient_id));
            keyValuePairs.Add(new KeyValuePair<string, string>("client_secret", AppsConfiguration.idaClient_secret));
            keyValuePairs.Add(new KeyValuePair<string, string>("grant_type", AppsConfiguration.grant_type_authorization_code));
            keyValuePairs.Add(new KeyValuePair<string, string>("code", code));
            keyValuePairs.Add(new KeyValuePair<string, string>("redirect_uri", AppsConfiguration.idaRedirect_uri));
            var client1 = new HttpClient();
            var req = new HttpRequestMessage(HttpMethod.Post, AppsConfiguration.idaTokenEndpoint) { Content = new FormUrlEncodedContent(keyValuePairs) };

            using (var res = client1.SendAsync(req).Result)
            {
                if (res.IsSuccessStatusCode)
                {
                    var jsonresult = JObject.Parse(await res.Content.ReadAsStringAsync());
                    token = (string)jsonresult["access_token"];
                    refresh_token = (string)jsonresult["refresh_token"];    
                }
            }

            return token;
        }

我是否需要在Azure AD V2应用程序中进行任何特定配置?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-12-04 15:30:22

我使用客户端终结点的登录URL为:https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={ v2 id}....

虽然这适用于Active Directory帐户和onmicrosoft帐户,但它不适用于Microsoft个人帐户,如hotmail/outlook。

对于我来说,当我将上面的登录URL更改为:https://login.microsoftonline.com/{域名/域名和id}/oauth2/v2.0/authorize?client_id={client id}...

谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53543694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档