【问题标题】:Asp.net Forms (VB.NET) Identity + MySQLAsp.net Forms (VB.NET) Identity + MySQL
【发布时间】:2018-06-13 17:22:15
【问题描述】:

Asp.net Forms (VB.NET) Identity + MySQL

您好,我正在尝试根据本教程编写代码。我想使用 MySql 和 Identity。 教程: https://docs.microsoft.com/en-us/aspnet/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider

目前我的代码如下所示:

Imports AspNet.Identity.MySQL
Imports Microsoft.Owin.Security
Imports Microsoft.AspNet.Identity

Public Class ApplicationUser
    Inherits IdentityUser

End Class

Public Class ApplicationDbContext
    Inherits MySQLDatabase

    Public Sub New(ByVal connectionName As String)
        MyBase.New(connectionName)
    End Sub

    Public Shared Function Create() As ApplicationDbContext
        Return New ApplicationDbContext("DefaultConnection")
    End Function
End Class


#Region "Helpers"
Public Class UserManager
    Inherits UserManager(Of ApplicationUser)
    Public Sub New()
           MyBase.New(New UserStore(Of ApplicationUser)(New ApplicationDbContext()))
    End Sub
End Class
Public Class IdentityHelper

    Public Const XsrfKey As String = "xsrfKey"

    Public Shared Sub SignIn(manager As UserManager, user As ApplicationUser, isPersistent As Boolean)
        Dim authenticationManager As IAuthenticationManager = HttpContext.Current.GetOwinContext().Authentication
        authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie)
        Dim identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie)
        authenticationManager.SignIn(New AuthenticationProperties() With {.IsPersistent = isPersistent}, identity)
    End Sub

    Public Const ProviderNameKey As String = "providerName"
    Public Shared Function GetProviderNameFromRequest(request As HttpRequest) As String
        Return request(ProviderNameKey)
    End Function

    Private Shared Function IsLocalUrl(url As String) As Boolean
        Return Not String.IsNullOrEmpty(url) AndAlso ((url(0) = "/"c AndAlso (url.Length = 1 OrElse (url(1) <> "/"c AndAlso url(1) <> "\"c))) OrElse (url.Length > 1 AndAlso url(0) = "~"c AndAlso url(1) = "/"c))
    End Function

    Public Shared Sub RedirectToReturnUrl(returnUrl As String, response As HttpResponse)
        If Not [String].IsNullOrEmpty(returnUrl) AndAlso IsLocalUrl(returnUrl) Then
            response.Redirect(returnUrl)
        Else
            response.Redirect("~/")
        End If
    End Sub
End Class
#End Region

但是有一个错误(或不止一个)

MyBase.New(New UserStore(Of ApplicationUser)(New ApplicationDbContext())

我不懂C#,教程很难继续下去:

如何在 VB 中使用它?

非常感谢。

【问题讨论】:

    标签: mysql asp.net vb.net forms identity


    【解决方案1】:

    尝试将其更改为以下内容:

    MyBase.New(New UserStore(Of ApplicationUser)(TryCast(context.[Get](Of ApplicationDbContext)(), MySQLDatabase)))
    

    【讨论】:

      猜你喜欢
      • 2012-04-05
      • 2016-05-06
      • 2021-04-19
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 2014-12-14
      • 2013-11-23
      相关资源
      最近更新 更多