VB.NET中的OWIN启动类

铁面人

我正在尝试使用 Microsoft 的示例项目作为参考为 Azure AD 身份验证创建 OWIN 启动类。该示例是在 C# 中,但我正在调整它的项目是在 VB.NET 中,所以我需要将以下 C# 转换为 VB.NET:

public class Startup
{
    // The Client ID is used by the application to uniquely identify itself to Azure AD.
    string clientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"];

    // RedirectUri is the URL where the user will be redirected to after they sign in.
    string redirectUri = System.Configuration.ConfigurationManager.AppSettings["RedirectUri"];

    // Tenant is the tenant ID (e.g. contoso.onmicrosoft.com, or 'common' for multi-tenant)
    static string tenant = System.Configuration.ConfigurationManager.AppSettings["Tenant"];

    // Authority is the URL for authority, composed by Azure Active Directory v2 endpoint and the tenant name (e.g. https://login.microsoftonline.com/contoso.onmicrosoft.com/v2.0)
    string authority = String.Format(System.Globalization.CultureInfo.InvariantCulture, System.Configuration.ConfigurationManager.AppSettings["Authority"], tenant);

    /// <summary>
    /// Configure OWIN to use OpenIdConnect 
    /// </summary>
    /// <param name="app"></param>
    public void Configuration(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                // Sets the ClientId, authority, RedirectUri as obtained from web.config
                ClientId = clientId,
                Authority = authority,
                RedirectUri = redirectUri,
                // PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page
                PostLogoutRedirectUri = redirectUri,
                Scope = OpenIdConnectScope.OpenIdProfile,
                // ResponseType is set to request the id_token - which contains basic information about the signed-in user
                ResponseType = OpenIdConnectResponseType.IdToken,
                // ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application
                // To only allow users from a single organizations, set ValidateIssuer to true and 'tenant' setting in web.config to the tenant name
                // To allow users from only a list of specific organizations, set ValidateIssuer to true and use ValidIssuers parameter 
                TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer = false
                },
                // OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthenticationFailed = OnAuthenticationFailed
                }
            }
        );
    }

    /// <summary>
    /// Handle failed authentication requests by redirecting the user to the home page with an error in the query string
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
    {
        context.HandleResponse();
        context.Response.Redirect("/?errormessage=" + context.Exception.Message);
        return Task.FromResult(0);
    }
}

这是我转换后的 VB.NET:

Public Class Startup
    Private clientId As String = ConfigurationManager.AppSettings("ClientId")
    Private redirectUri As String = ConfigurationManager.AppSettings("RedirectUri")
    Shared tenant As String = ConfigurationManager.AppSettings("Tenant")
    Private authority As String = String.Format(Globalization.CultureInfo.InvariantCulture, ConfigurationManager.AppSettings("Authority"), tenant)

    Public Sub Configuration(ByVal app As IAppBuilder)
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType)
        app.UseCookieAuthentication(New CookieAuthenticationOptions())

        app.UseOpenIdConnectAuthentication(New OpenIdConnectAuthenticationOptions With {
            .ClientId = clientId,
            .Authority = authority,
            .RedirectUri = redirectUri,
            .PostLogoutRedirectUri = redirectUri,
            .Scope = OpenIdConnectScope.OpenIdProfile,
            .ResponseType = OpenIdConnectResponseType.IdToken,
            .TokenValidationParameters = New TokenValidationParameters() With {
                .ValidateIssuer = False
            },
            .Notifications = New OpenIdConnectAuthenticationNotifications With {
                .AuthenticationFailed = OnAuthenticationFailed() '<---ERROR HERE
            }
        })
    End Sub

    Private Function OnAuthenticationFailed(ByVal context As AuthenticationFailedNotification(Of OpenIdConnectMessage, OpenIdConnectAuthenticationOptions)) As Task
        context.HandleResponse()
        context.Response.Redirect("/?errormessage=" & context.Exception.Message)
        Return Task.FromResult(0)
    End Function
End Class

但是,我在线上遇到了错误.AuthenticationFailed = OnAuthenticationFailed()错误说我需要传入context有意义参数,但我对为什么 C# 版本不需要它以及我应该在 VB.NET 中传递什么感到困惑。

谢谢!

铁面人

我最终只是编写了OnAuthenticationFailed()内联函数:

Public Sub Configuration(ByVal app As IAppBuilder)
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType)
        app.UseCookieAuthentication(New CookieAuthenticationOptions())

        app.UseOpenIdConnectAuthentication(New OpenIdConnectAuthenticationOptions With {
            .ClientId = clientId,
            .Authority = authority,
            .RedirectUri = redirectUri,
            .PostLogoutRedirectUri = redirectUri,
            .Scope = OpenIdConnectScope.OpenIdProfile,
            .ResponseType = OpenIdConnectResponseType.IdToken,
            .TokenValidationParameters = New TokenValidationParameters() With {
                .ValidateIssuer = True
            },
            .Notifications = New OpenIdConnectAuthenticationNotifications With {
                .AuthenticationFailed = Function(context)
                                            context.HandleResponse()
                                            context.Response.Redirect(HttpContext.Current.Server.MapPath("~") & "?errormessage=" & context.Exception.Message)
                                            Return Task.FromResult(0)
                                        End Function
            }
        })
End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Hangfire和VB.NET-在“应用程序启动”类中配置事物

来自分类Dev

在VB.net中创建VBA类

来自分类Dev

Vb.net中的Mediator Helper类

来自分类Dev

处置类vb.net

来自分类Dev

生成类 VB.NET

来自分类Dev

使用vb.net中包含空格的参数启动exe

来自分类Dev

selenium 如何启动隐藏在 vb.net 中的 chrome?

来自分类Dev

从vb.net中的类列表中永久删除项目

来自分类Dev

从VB.Net中的“类”模块集合中检索信息

来自分类Dev

将vb.net类对象转换为vb.net中的JSON字符串

来自分类Dev

比较-VB类中的列表

来自分类Dev

在vb.net中对类数组进行排序

来自分类Dev

在Mustinherit类中返回自类型-VB.NET

来自分类Dev

如何确定通用类VB.NET中的类型

来自分类Dev

VB.Net从模块与类中的函数返回数据集

来自分类Dev

在VB.Net中手动处置类实例

来自分类Dev

在Mustinherit类中返回自类型-VB.NET

来自分类Dev

类类型作为vb.net中的函数参数

来自分类Dev

从VB.Net中的窗体类外部更改光标

来自分类Dev

从VB.Net中的窗体类外部更改光标

来自分类Dev

VB.net从类创建的数组中检索特定值?

来自分类Dev

VB.net中的SQL连接类出现问题

来自分类Dev

VB.NET - 在 VBE 中解释为类的模块

来自分类Dev

如何在类中添加单击事件 vb.net

来自分类Dev

我无法在 vb.net 中设置引导程序类

来自分类Dev

VB6使用VB.NET类-它很慢

来自分类Dev

vb.net如何启动Minecraft?

来自分类Dev

以vb.net顺序启动的多个线程

来自分类Dev

将方法添加到VB.Net中的.Net类库的类中?