[授权]成功后,OpenIDConnect SSO出现问题

问题描述

我仍在学习MVC身份验证,因此我深表歉意。 我正在尝试将OpenIDConnect与Azure AD一起使用来设置SSO,并且将基于Microsoft在Azure网站快速入门中提供的示例。具体来说,打电话

app.USEOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationoptions
    { 
        //options 
    }

然后使用[Authorize]属性用户重定向到Microsoft登录名。这行得通,我得到了用户的要求。

但是,当我将RedirectUrl添加到OpenIdConnectAuthenticationoptions中时,它似乎被忽略了,并且请求在进行身份验证后返回到启动操作,而不是进行回调。

当我通过SignIn发出直接质询时,重定向工作正常,但这将要求我以某种方式将对页面的任何“未经授权”访问重定向到该SignIn方法。我认为,如果[授权]可以完成相同的工作,那会更清洁。 在线阅读表明,[授权]之后有意返回到基本操作,因此我认为我在这里没有做错什么。

在任何情况下,如果[Authorize]都可以,那就很好,除了在授权成功之后,我要运行一些自定义代码(设置一些自定义Cookie值/ Session变量,检查数据库以使用户与我们的系统相匹配,这样的事情),然后再进行带有[Authorize]标签的实际操作。 我不想在他们“登录”时就每次请求都这样做。 理想情况下,我将能够在事件内访问新验证的Claims,甚至可以访问ExternalLoginInfo对象。

是否有事件或可以为此附加一些代码的东西?

我找到了“ SecurityTokenValidated”,但该方法中的User和LoginInfo均为空。

我还尝试了等[Authorize]完成后,直到我回到基本Action中。我有用户,但没有ExternalLoginInfo,并且我不想将“ post-auth”代码复制/粘贴到用户可以到达站点的每个操作中。

这是我的创业公司。Auth.cs:

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

app.UseCookieAuthentication(new CookieAuthenticationoptions());

// I tried creating a second temporary cookie with WebCookieManager,that didn't help.
//app.UseCookieAuthentication(new CookieAuthenticationoptions
//{
//    AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,//    CookieName = OWIN_COOKIE_PREFIX + DefaultAuthenticationTypes.ApplicationCookie,//    ExpireTimeSpan = TimeSpan.FromMinutes(5),//    CookieManager = new SystemWebCookieManager()
//});

app.USEOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationoptions
    {
        ClientId = clientId,Authority = authority,RedirectUri = postLoginRedirectUri,//This seems to get ignored anyways in favor of returning to the [Authorize]-marked Action.
        Scope = OpenIdConnectScopes.OpenIdProfile,ResponseType = OpenIdConnectResponseTypes.IdToken,TokenValidationParameters = new TokenValidationParameters() { ValidateIssuer = true },Notifications = new OpenIdConnectAuthenticationNotifications 
        { 
            SecurityTokenValidated = OnAuthenticationSuccess
        }
    });

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)