繁体   English   中英

防止在Asp.NET MVC中使用OpenId Connect登录

[英]Prevent login using OpenId Connect in Asp.NET MVC

我已经运行了OpenIdConnect服务(Thinktecture IdSvr),并且可以完美登录我的应用程序。

当用户使用OpenId Connect成功登录时,我想检查它们是否存在于客户端用户存储中,并允许他们继续或注销它们。

为此,我正在处理OpenIdConnectAuthenticationOptions.NotificationsAuthorizationCodeReceived事件,但无法确定如何阻止用户成功登录。

我假设不设置n.AuthenticationTicket将阻止创建原则。

var userInfoClient = GetUserInfoClient();

var userInfo = await userInfoClient.GetAsync();
var email = GetEmailAddr(userInfo);

var user = GetClientUser(email)

if (user != null)
{
    // access and refresh token
    var tokenClient = GetTokenClient();

    var tokenResponse = await GetToken(tokenClient);

    claims.Add(new Claim("access_token", tokenResponse .AccessToken));
    claims.Add(new Claim("expires_at", DateTime.Now.AddSeconds(tokenResponse .ExpiresIn).ToLocalTime().ToString()));
    claims.Add(new Claim("refresh_token", tokenResponse .RefreshToken));
    claims.Add(new Claim("id_token", n.ProtocolMessage.IdToken));

    n.AuthenticationTicket = new AuthenticationTicket(
        new ClaimsIdentity(
            claims.Distinct(new ClaimComparer()),
            n.AuthenticationTicket.Identity.AuthenticationType),
            n.AuthenticationTicket.Properties);
}

这很简单。 我所做的只是在AuthorizationCodeReceived事件中引发了一个新的SecurityException

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM