我有一个.net Web API,我为SPA提供了一个 endpoints ,以获取Azure oAuth 2访问令牌 . 当我在本地运行web api时,服务正常 . 但是,当我将服务发布到MS Azure时, endpoints 返回给我{“消息”:“发生了错误 . ”} . 仅在我要对用户进行身份验证时才会引发错误 . 如果我为具有客户端密钥的客户端身份验证运行相同的代码,则会正确返回访问令牌 .
我的问题是,为什么这只发生在azure上,我该如何解决?
User Auth (不在Azure上工作)
资源是例如:“https://graph.microsoft.com”
public string Get([FromUri] String resource)
{
var token = GetAuthenticatedUserAsync(resource).Result;
return token.AccessToken;
}
//Authentication of an user
[DisableCors()]
private static async Task<Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult> GetAuthenticatedUserAsync(String resource)
{
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/{tenant}/oauth2/authorize");
return await authContext.AcquireTokenAsync(resource, ConfigurationManager.AppSettings["UserClientId"], new Uri("{redirect url}"), new PlatformParameters(PromptBehavior.Auto)).ConfigureAwait(false);
}
Client Auth (在Azure上工作)
//Authentication of a client
[DisableCors()]
public static async Task<Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult> GetAuthenticatedAppAsync()
{
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/{tenant}/oauth2/authorize");
var credentials = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(ConfigurationManager.AppSettings["AppClientId"], ConfigurationManager.AppSettings["AppClientSecret"]);
return await authContext.AcquireTokenAsync(ConfigurationManager.AppSettings["GraphResource"], credentials).ConfigureAwait(false);
}
1 回答
由于juunas提到我们无法在Azure WebApp上使用 PromptBehavior.Auto 方式 .
似乎 PromptBehavior 是GDI行为 . 因为WebApp是sandbox . Azure WebApp中不允许使用它 .
如果要在Azure应用服务中端到端地对用户进行身份验证和授权,请参阅此tutorial和此tutorial .