【发布时间】:2015-12-08 05:29:42
【问题描述】:
刚刚遇到了一个关于 azure 广告应用程序和 owin openid 身份验证的奇怪问题。 重现问题。
1.在 vs 2015 中选择云应用模板创建一个具有 azure 广告身份验证的网络应用。
2.让标准代码保持原样。
3.让 startup.auth 保持原样。
4.在本地运行应用程序运行良好。
5.现在在启动àuth中更改代码如下
public partial class Startup
{
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"];
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
public static readonly string Authority = aadInstance + tenantId;
// This is the resource ID of the AAD Graph API. We'll need this to request a token to call the Graph API.
string graphResourceId = "https://graph.windows.net";
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void ConfigureAuth(IAppBuilder app)
{
ApplicationDbContext db = new ApplicationDbContext();
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
logger.Debug("SetDefaultSignInAsAuthenticationType called");
//app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseCookieAuthentication(
new CookieAuthenticationOptions
{
Provider = new CookieAuthenticationProvider
{
OnResponseSignIn = ctx =>
{
//logger.Debug("OnResponseSignIn called");
////ctx.Identity = TransformClaims(ctx.Identity);
//logger.Debug("TransformClaims called");
}
}
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
// If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(clientId, appKey);
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
logger.Debug("OnResponseSignIn called");
logger.Debug("signedInUserID =" + signedInUserID);
TransformClaims(context.AuthenticationTicket.Identity);
logger.Debug("TransformClaims called");
AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID));
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
return Task.FromResult(0);
},
// we use this notification for injecting our custom logic
SecurityTokenValidated = (context) =>
{
logger.Debug("SecurityTokenReceived called");
//TransformClaims(); //pass the identity
return Task.FromResult(0);
},
}
});
}
private static void TransformClaims(System.Security.Claims.ClaimsIdentity identity)
{
if (identity != null && identity.IsAuthenticated == true)
{
var usserobjectid = identity.FindFirst(ConfigHelpers.Azure_ObjectIdClaimType).Value;
((System.Security.Claims.ClaimsIdentity)identity).AddClaim(new System.Security.Claims.Claim("DBID", "999"));
((System.Security.Claims.ClaimsIdentity)identity).AddClaim(new System.Security.Claims.Claim("Super","True"));
}
// return identity;
}
}
6.在本地运行应用程序将完美运行。
7.在 azure 网站上部署应用程序,并且永远不会调用启动 àuth owin 通知方法。但是应用程序可以工作,但身份转换不能
有人可以帮忙看看这是什么问题吗?天蓝色的广告应用不支持 cookie 或通知未触发或代码有任何问题。
只是为了重新断言而不是startup.àuth 没有更改标准代码。
【问题讨论】:
-
尝试删除 logger.Debug("OnResponseSignIn called");线并再次部署。有时 trace.writes 在 Azure 中托管时可能会导致问题,具体取决于您的跟踪侦听器是什么...
-
我添加了 logger.debug("OnResponseSignin Called") 来记录所有事件是否触发,如果这可能是问题,它可能无法在 localhost 上完美运行,等等只知道发生了什么我添加了 log4net 记录器,但它仍然在 localhost 而不是 azure 网站上运行完美。所以我敢肯定,这不是问题。你可以重现它,我已经粘贴了完整的代码。
-
这将在本地工作,但在天蓝色它不会...尝试进行远程调试以查看问题...
-
一切准备就绪,试了几次,终于贴出来看看有什么问题。 logger 不是问题,忽略它。问题是没有通知被触发,因此不会发生身份转换。
-
唯一阻止通知触发的是身份验证过程没有达到那么远(例如,早期出现问题)。然而,您声称登录工作正常?其中之一应该是错误的。尝试连接所有通知以查看该过程的进展情况。您还可以通过做一些更戏剧性的事情来证明通知触发了,比如抛出一个自定义异常。
标签: azure owin azure-active-directory katana