【发布时间】:2016-04-27 09:14:01
【问题描述】:
让我用一点结构来解决这个问题。
上下文
我们有一个使用 Web 表单构建并托管在 Azure Web 应用程序中的 Web 应用程序,该应用程序使用 OWIN + OpenId Connect 标准针对 Azure Active Directory 对用户进行身份验证。
身份验证过程就像一个魅力,用户可以毫无问题地访问应用程序。
那么,问题出在哪里?
在苦苦挣扎了很多天之后,我无法通过身份验证过程将任何查询字符串参数传递给应用程序。例如,如果我第一次尝试通过 URL 访问应用程序:https://myapp.azurewebsites.net/Default.aspx?param=value。我需要传递这个参数的原因是它会在主页中触发一些特定的操作。
问题是认证重定向到webapp的主页后,请求的原始查询字符串参数消失了。
代码
启动类如下所示:
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = Constants.ADTenant.ClientId,
Authority = Constants.ADTenant.Authority,
PostLogoutRedirectUri = Constants.ADTenant.PostLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthorizationCodeReceived = context =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(Constants.ADTenant.ClientId,
Constants.ADTenant.AppKey);
string userObjectID = context.AuthenticationTicket.Identity.FindFirst(
Constants.ADTenant.ObjectIdClaimType).Value;
AuthenticationContext authContext = new AuthenticationContext(Constants.ADTenant.Authority,
new NaiveSessionCache(userObjectID));
if (HttpContext.Current != null)
{
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential,
Constants.ADTenant.GraphResourceId);
AuthenticationHelper.token = result.AccessToken;
AuthenticationHelper.refreshToken = result.RefreshToken;
}
return Task.FromResult(0);
}
}
});
而且它工作正常!
我已经尝试过的
通过添加RedirectToIdentityProvider 通知的覆盖,我可以访问原始请求网址:
RedirectToIdentityProvider = (context) =>
{
// Ensure the URI is picked up dynamically from the request;
string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase + context.Request.Uri.PathAndQuery;
context.ProtocolMessage.RedirectUri = appBaseUrl;
return Task.FromResult(0);
}
通过这个,我尝试强制重定向到包含原始查询字符串参数的主页,但是身份验证后的重定向中断并陷入无限循环。
我还尝试在 Azure AD 中更改应用程序配置的重定向 url,但没有成功。还尝试将查询字符串参数存储在其他位置,但在此过程的早期无法访问 Session。
有谁知道我做错了什么?或者我只是要求一些不可能的事情?任何帮助将不胜感激。
非常感谢您!
【问题讨论】:
标签: c# asp.net azure authentication owin