如何使用 Microsoft.Owin 获取失败身份验证的错误原因?

问题描述 投票:0回答:1

我在 Startup.Auth.cs 中有以下代码:

app.UseOpenIdConnectAuthentication(

                        new OpenIdConnectAuthenticationOptions
                        {
                            ClientId = myclientId,
                            Authority = authority,
                            PostLogoutRedirectUri = mypostLogoutRedirectUri,
                           ,

                            

                            Notifications = new OpenIdConnectAuthenticationNotifications
                            {
                                AuthenticationFailed = (context) =>
                                {
                                    context.HandleResponse();
                                    string error = ""; //<-- what goes here?
                                    context.OwinContext.Response.Redirect("/Home/auth_error?error_msg=" + error);
                                    return Task.FromResult(0);
                                },
                                SecurityTokenValidated = (context) =>
                                {
                                    string name = context.AuthenticationTicket.Identity.Name;
                                    context.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimTypes.Name, name, string.Empty));
                                    return System.Threading.Tasks.Task.FromResult(0);
                                }
                            }
                        });

我想要做的是显示一条错误消息,说明身份验证失败的原因。我如何以字符串形式获取

AuthenticationFailed
段中的所有适当信息?

c# asp.net-mvc azure-active-directory owin
1个回答
0
投票

您的上下文应在

context.Exception.Message
中包含您的消息。

参见:https://learn.microsoft.com/en-us/previous-versions/aspnet/mt180967(v=vs.113)

© www.soinside.com 2019 - 2024. All rights reserved.