OpenIdConnect重定向,就好像用户未登录

时间:2018-10-29 22:30:08

标签: c# asp.net-mvc authentication cookies openid-connect

我正在为客户开发一个MVC项目(我们称其为“项目A”),我们必须使用OpenIdConnect进行授权(基本上可以使它正常工作),如果我正在将其重定向到登录站点,未经授权等。但是,如果我想使用[Authorize]进行操作,它将向OpenIdConnect-Authority发出请求,以查看我是否已登录,然后将我重定向到为此客户端指定的RedirectUrl
因此,如果我想去/Home/Contact(在我的例子中是[Authorize]),我将无法到达那里,因为我将始终被重定向到我的“ RedirectUrl”。

我是否必须自己存储请求的网址,然后自己重​​定向? 是否设置RedirectUrl都没有关系。我认为这有点奇怪,因为在另一个项目B中,我们使用了Identity Server 4,并且在那里工作没有任何问题。

这是我的Startup.Auth.cs中的代码:

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
});

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions
    {
        ClientId = clientId,
        ClientSecret = clientSecret,
        Authority = authority,
        RedirectUri = redirectUri,
        ResponseType = "code",
        Scope = "openid profile email",
        Configuration = new Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfiguration
        {
            AuthorizationEndpoint = authority + "as/authorization.oauth2"
        },
});

我必须自己设置AuthorizationEndpoint,因为它始终使用错误的URL。在项目A和项目B之间的区别在于,在项目A中将“ ResponseType”设置为“ code”。在项目B中将其设置为“ id_token”。但是我必须将其设置为“代码”,因为不接受其他响应类型。

在项目B中,我还注意到“ .AspNet.Cookies”在我的网站上可用。 项目A中不是这种情况。我必须自己创建它吗?

0 个答案:

没有答案