首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用Bearer令牌验证web api

无法使用Bearer令牌验证web api
EN

Stack Overflow用户
提问于 2016-05-11 13:29:37
回答 1查看 918关注 0票数 0

我目前正在从事一个项目,其中我有一个Web,它使用Graph在Azure AD中创建帐户,并将它们放在正确的组中。

接下来,我公开了几个API调用,这些调用将被本地iOS应用程序和angularJS web应用程序调用。客户端编造了一些奇怪的方法来进行身份验证,因为他坚信自己的用户是十足的白痴。

客户端正在将定制的准备好的iOS设备交给某些人。该设备具有Azure AD用户(主体)和密码。一旦它们被分发出去,angularJS web应用程序上的一些进程就会这样做,然后应用程序将调用我的令牌控制器,如下所示:

代码语言:javascript
复制
    public async Task<string> GetTokenForUserAsync(string userPrincipalName, string password)
    {
        var uri = string.Format("{0}/oauth2/token", AuthString);
        using (var httpClient = new HttpClient
        {
            DefaultRequestHeaders = { Accept = { new MediaTypeWithQualityHeaderValue("application/json") } }
        })
        {
            var values = new Dictionary<string, string>
            {
                {"resource", GraphUrl },
                {"client_id", ClientId },
                {"client_secret", ClientSecret },
                {"grant_type", "password" },
                {"username", userPrincipalName },
                {"password", password },
                {"scope", "openid" }
            };

            var content = new FormUrlEncodedContent(values);
            var response = await httpClient.PostAsync(uri, content);

            var responseContent = await response.Content.ReadAsStringAsync();

            return responseContent;
        }

传递的参数不是100%准确,但非常相似:

因此,这个调用实际上为我提供了一个access_token。我遇到的问题是,我得到的代币从未被授权过。我试过几个创业机构,但都没起作用。

目前,我的Startup.Auth.cs中有以下代码

代码语言:javascript
复制
    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
        {
            TokenValidationParameters = new TokenValidationParameters
            {
                ValidAudience = ConfigurationManager.AppSettings["ida:ClientId"]
            }

        });
        //app.UseWindowsAzureActiveDirectoryBearerAuthentication(
        //    new WindowsAzureActiveDirectoryBearerAuthenticationOptions
        //    {
        //        TokenValidationParameters = new TokenValidationParameters
        //        {
        //            ValidAudience = ConfigurationManager.AppSettings["ida:ClientId"]
        //        },
        //        Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
        //    });
    }

这是我第一次使用Azure和Active Directory。所以我很可能做了件很愚蠢的事。在这个时候,我不太关心造型和‘正确的方式’。我只想让这个东西发挥作用

希望我正确地描述了我的问题,并相应地记录了我的问题。如果你有任何问题,请不要犹豫地问!

先谢一堆

EN

回答 1

Stack Overflow用户

发布于 2016-05-12 09:47:42

我认为您无法从var value=新字典{“资源”,GraphUrl },{"client_id",ClientId },{"client_secret",ClientSecret },{"grant_type",“密码”} },{“用户名”,userPrincipalName },{“密码”,密码},{“范围”、"openid“};因此,您可以尝试其他方法:

代码语言:javascript
复制
       AuthenticationContext aContext =
                new AuthenticationContext("https://login.microsoftonline.com/tenantid");
            AuthenticationResult aResult =
                aContext.AcquireToken("https://graph.windows.net",
                                "1950a258-227b-4e31-a9cf-717495945fc2",
                                new UserCredential(UserId, PasswordId));

            string result = string.Empty;
            HttpClient httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Bearer", aResult.AccessToken);
            HttpResponseMessage response =
                httpClient.GetAsync("https://graph.windows.net/tenantid/users/userid?api-version=1.6").Result;

            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsStringAsync().Result;
            }
            Console.WriteLine(result);
            Console.ReadLine();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37164205

复制
相关文章

相似问题

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