Lui*_*reu 2 asp.net-mvc azure-active-directory openid-connect
I'm trying to build a ASP.NET MVC app which doesn't allow access to anonymous user (with the exception of a custom URL that is to be shown to authenticated users which aren't allowed to use the app).
Now, I've register my app in the azure portal (portal.azure.com) and I'd like to use several URLs. I've added two entries:
I'm using the following code to configure authentication at startup:
public void ConfigureAuth(IAppBuilder app) {
app.SetDefaultSignInAsAuthenticationType(
CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions {
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications {
SecurityTokenValidated = VerificaUtilizadorAutenticado,
AuthenticationFailed = TrataErroAutenticacao
}
});
}
Run Code Online (Sandbox Code Playgroud)
Everything seemed to be working fine, but after publishing the app, it seems like I can only use one of the URIs. I've searched and it seems like I can use the RedirectUri property of the OpenIdConnectAuthenticationOptions to set up the reply url. So, I've tried adding this to the setup:
RedirectUri = "https://localhost/test",
Run Code Online (Sandbox Code Playgroud)
不幸的是,这样做破坏了一切,并且浏览器卡在了我的应用程序和MS的登录页面之间。由于用户已登录,因此它将用户重定向回我的应用程序。但是,似乎设置RedirectUri prop不会生成应用程序的身份验证cookie,因此它将用户发送回登录页面。
如果删除RedirectUri,即使我尝试访问https:// localhost / test Web应用程序,我也会重定向到https://www.test.com站点。
我不知道该怎么办,但我想念一些东西...有人可以帮忙吗?
谢谢。
路易斯
好吧,经过一番挖掘,我注意到我在设置重定向时遇到的问题是我缺少......斜线(/)。因此,将 / 添加到门户上定义的 URL 并将其也放入选项对象的重定向中就足以使其适用于不同的 URI...
当您发送要登录到AAD登录端点(https://login.microsoftonline.com)的用户时,您将需要指定要将用户(和授权代码)重定向到的位置。
您可以通过两种方式指定它:
您必须在登录URL中指定一个答复URL,该URL与您配置的其中一个URL完全匹配:
https://login.microsoftonline.com/{tenant}/oauth2/authorize?
client_id=6731de76-14a6-49ae-97bc-6eba6914391e
&response_type=code
&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F <-- this guy
&response_mode=query
&resource=https%3A%2F%2Fservice.contoso.com%2F
&state=12345
Run Code Online (Sandbox Code Playgroud)如果您未在登录URL中指定重定向URI,则会将用户重定向到应用程序注册中指定的第一个URL。听起来还不错,但是您知道AAD会将您的Reply URL视为无序列表,因此,根据您命中的服务器或数据的复制方式,您可能会在不同的登录尝试之间观察到不同的重定向行为。这就是为什么始终redirect_uri在登录端点中指定参数很重要的原因。
我觉得,如果您使用ADAL库而不是示例代码中显示的库进行身份验证,则可能会解决许多问题,但是,如果您不想使用这些代码,则需要共享更多详细信息,例如HTTP跟踪认证过程。
| 归档时间: |
|
| 查看次数: |
3931 次 |
| 最近记录: |