简体   繁体   English

ADAL.NET Winforms应用程序

[英]ADAL.NET winforms application

I have a asp.net web api 2 which uses AAD(Azure AD) to authenticate users. 我有一个使用AAD(Azure AD)来验证用户身份的asp.net Web API 2。 Currently i only have a angular web application accessing the api. 目前,我只有一个有角度的Web应用程序访问api。 The authentication of the users is being handled by adal.js, this works. 用户的身份验证由adal.js处理,这可行。 Now im trying to add a winforms application, that also authenticates against the same AAD. 现在,我试图添加一个winforms应用程序,该应用程序还针对同一AAD进行身份验证。 This also works, meaning i get an auth-token, but when i call the API it returns a 401 message. 这也有效,这意味着我得到一个身份验证令牌,但是当我调用API时,它将返回401消息。 I have looked at the Azure AD samples at github and have based my code on them. 在github上查看了Azure AD示例,并基于它们编写了代码。

In the web api, i have the following code in the Startup.Auth.cs: 在Web API中,我在Startup.Auth.cs中具有以下代码:

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

The audience and Tenant key are copied from Azure AD. 受众和租户密钥是从Azure AD复制的。

I use the following code in the Winforms Application: 我在Winforms应用程序中使用以下代码:

    string authority = "https://login.microsoftonline.com/{TenantID}";
    string resourceURI = "APP ID URI from the Web API registered in Azure AD";
    string clientID = "Client ID from the WinForms Application registered in Azure AD";
    Uri returnUri = new Uri("Registered return uri");
    AuthenticationContext _ac = new AuthenticationContext(authority);
    AuthenticationResult _arr = _ac.AcquireToken(resourceURI, clientID, returnUri);

To recap, i can authenticate an user in the winforms application, but when i request data from the API, the api responds with a 401 error. 回顾一下,我可以在winforms应用程序中对用户进行身份验证,但是当我从API请求数据时,api会响应401错误。 I have checked the http request and the token is being injected into the request. 我已经检查了http请求,并且令牌已注入到请求中。

Thank you guys for you help 谢谢你们的帮助

Did you make sure you associated both applications (the Web API and the WinForm application) with an AAD application, and that you have granted permissions to the WinForm application to access the Web API? 您是否确定将两个应用程序(Web API和WinForm应用程序)都与AAD应用程序相关联,并且已授予WinForm应用程序访问Web API的权限?

A good example is provided in https://github.com/Azure-Samples/active-directory-dotnet-native-multitarget . https://github.com/Azure-Samples/active-directory-dotnet-native-multitarget中提供了一个很好的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM