c# - 在 ASP.Net MVC 中使用 OpenIdConnect 进行身份验证后重定向用户

标签 c# asp.net-mvc-5 owin openid-connect identityserver3

我在我的 asp.net mvc 应用程序中使用带有 Owin/Katana 的 OpenIdConnect 提供程序进行身份验证。 OpenIdConnect Provide 根据 Active Directory 对用户进行身份验证。我想在用户通过身份验证后进行简单的授权检查,并将用户重定向到另一个 View 。

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions()
        {
            Authority = "url",
            Scope="scopes",
            ResponseType = "response",
            ClientId = "clientid",
            SignInAsAuthenticationType = "Cookies",
            Notifications = new OpenIdConnectAuthenticationNotifications()
            {
                SecurityTokenValidated = (context) =>
                {
                    var identity = context.AuthenticationTicket.Identity;
                    var emailClaim = identity.Claims.Where(r => r.Type == ClaimTypes.Email).FirstOrDefault();

                    var user = dbContext.Users.Where(u=>u.Email==emailClaim.Value);
                    if (user != null)
                    {
                        //add user information to claims.
                        identity.AddClaim(new Claim(CustomClaimTypes.PersonId, user.Name.ToString()));
                    }
                    else
                    {
                        //redirect to a page 
                    }

                    return Task.FromResult(0);
                }
             }
        });

如果他不在我的数据库中,我如何重定向用户。

最佳答案

添加到已接受的答案中,以防有人像我一样与此作斗争。我发现以下选项对我有用 -

选项 1

//redirect to a page 
context.AuthenticationTicket.Properties.RedirectUri = "Url";

选项 2

//redirect to a page      
context.HandleResponse();
context.Response.Redirect("/Error?message=" + context.Exception.Message);

请注意,第二个选项导致我的 HttpContext.User.Identity 为空。我想是因为 HandlResponse 停止了所有处理。如果这不是问题,仍然有用。

关于c# - 在 ASP.Net MVC 中使用 OpenIdConnect 进行身份验证后重定向用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32488171/

相关文章:

c# - 在后台加载一个窗口,直到告诉它显示

c# - 使用 C# 的 MacOs 事件窗口标题

c# - 设置一个函数等于某物

用于验证属性路由正则表达式约束中不带引号的字母数字 csv 列表的正则表达式

c# - Microsoft Practice 使用 Unity 寄存器类型

oauth - owin和oAuth2.0有什么关系?

asp.net-mvc - OWIN认证中间件 : logging off

c# - WPF:关键语法中的 ResourceKeys 如何工作?

javascript - 如何使用模型绑定(bind)器将数据从表单+跨度值发布到数据库

c# - 根据要求修改 OWIN/Katana PhysicalFileSystem 页面