在VB中使用ASP.NET标识

时间:2015-06-11 22:13:07

标签: asp.net vb.net webforms asp.net-identity

我已经广泛研究过谷歌和这个网站,但找不到答案。我正在尝试将ASP.NET身份添加到Visual Studio,我一直在使用this page

问题是,该文章的所有内容都在C#中,我的项目特别在Visual Basic中(ASP.NET Framework 4.5.1,不是 MVC)。此外,我对网络开发人员来说太新了,无法确切知道要改变什么。我发现了一个C#到VB的转换器,它在大多数情况下都有效但我仍然遗漏了一些东西,因为我在VS中遇到了错误。我甚至不能100%确定我可以使用VB而不是C#来使用Identity,尽管我不明白为什么不这样做。

我正在尝试尽可能简洁,所以我会用粗体表示问题。

这是register.aspx页面:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Register.aspx.vb" Inherits="WebFormsIdentity.Register" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="font-family: Arial, Helvetica, sans-serif; font-size: small">
    <form id="form1" runat="server">
    <div>
        <h4 style="font-size: medium">Register a new user</h4>
        <hr />
        <p>
            <asp:Literal runat="server" ID="StatusMessage" />
        </p>                
        <div style="margin-bottom:10px">
            <asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
            <div>
                <asp:TextBox runat="server" ID="UserName" />                
            </div>
        </div>
        <div style="margin-bottom:10px">
            <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
            <div>
                <asp:TextBox runat="server" ID="Password" TextMode="Password" />                
            </div>
        </div>
        <div style="margin-bottom:10px">
            <asp:Label runat="server" AssociatedControlID="ConfirmPassword">Confirm password</asp:Label>
            <div>
                <asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" />                
            </div>
        </div>
        <div>
            <div>
                <asp:Button runat="server" OnClick="CreateUser_Click" Text="Register" />
            </div>
        </div>
    </div>
    </form>
</body>
</html>

VB中没有错误,但在运行页面后,我在浏览器中遇到“Parser Error”:“分析器错误消息:无法加载类型'WebFormsIdentity.Register '。”

这是背后的代码;大多数问题都与此页面有关:

Imports Microsoft.AspNet.Identity
Imports Microsoft.AspNet.Identity.EntityFramework
Imports System.Linq

Namespace WebFormsIdentity
    Partial Public Class Register
        Inherits System.Web.UI.Page
        Protected Sub CreateUser_Click(sender As Object, e As EventArgs)
            ' Default UserStore constructor uses the default connection string named: DefaultConnection
            Dim userStore = New UserStore(Of IdentityUser)()
            Dim manager = New UserManager(Of IdentityUser)(userStore)

            Dim user = New IdentityUser() With { _
                Key .UserName = UserName.Text _
            }
            Dim result As IdentityResult = manager.Create(user, Password.Text)

            If result.Succeeded Then
                StatusMessage.Text = String.Format("User {0} was created successfully!", user.UserName)
            Else
                StatusMessage.Text = result.Errors.FirstOrDefault()
            End If
        End Sub
    End Class
End Namespace

在“Dim user”下的行上,“Key”一词出现此错误:

  

在对象初始值设定项中初始化的字段或属性的名称必须以“。”开头。

在“昏暗结果”一行中,“密码”一词包含:

  

未声明'密码'。由于其保护级别,它可能无法访问

在'result.Succeeded'下的行上,'StatusMessage'一词包含:

  

未声明“StatusMessage”。由于其保护级别,它可能无法访问。

最后,同一行'user.UserName'有这个:

  

'UserName'不是'System.Security.Principal.IPrincipal'的成员。

我只使用VB大约六周,所以我是一个小宝贝。我确实知道如何声明事物,但我不明白为什么上述链接的作者不必在他的代码中做任何额外的事情,除了它是C#。

无论如何,感谢您的阅读和提前协助。

2 个答案:

答案 0 :(得分:3)

行。 首先删除单词“KEY” - 它来自Telerik转换器 - 我不知道原因。 第二。在您的HTML代码上重复ID = UserName和ID =密码win name =“UserName”和name =“Password” 希望这会有所帮助。

答案 1 :(得分:3)

我能够解决我的问题。步骤进行:

1)完全卸载VS 2013 Premium
2)安装VS 2013 Ultimate
3)在开始一个新项目时,我选择了“Web表格”而不是使用空模板。使用默认的“个人用户帐户”#39;验证。
4)从NuGet包中安装text dump

运行应用程序然后正常工作;我能够没有任何错误地注册用户。

供将来参考,正确的代码如下。请注意,&#39; Owin&#39;必须导入参考文献。

<强> Register.aspx:

Imports System
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports Microsoft.AspNet.Identity
Imports Microsoft.AspNet.Identity.EntityFramework
Imports Microsoft.AspNet.Identity.Owin
Imports Owin

Partial Public Class Register
    Inherits Page
    Protected Sub CreateUser_Click(sender As Object, e As EventArgs)
        Dim userName As String = Email.Text
        Dim manager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)()
        Dim signInManager = Context.GetOwinContext().Get(Of ApplicationSignInManager)()
        Dim user = New ApplicationUser() With {.UserName = userName, .Email = userName}
        Dim result = manager.Create(user, Password.Text)
        If result.Succeeded Then
            ' For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
            ' Dim code = manager.GenerateEmailConfirmationToken(user.Id)
            ' Dim callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request)
            ' manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=""" & callbackUrl & """>here</a>.")

            signInManager.SignIn(user, isPersistent := False, rememberBrowser := False)
            IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response)
        Else
            ErrorMessage.Text = result.Errors.FirstOrDefault()
        End If
    End Sub
End Class

<强> Register.aspx.vb:

{{1}}