2015-06-11 69 views
0

我已經廣泛研究了Google和此站點,並且找不到答案。我正在嘗試將ASP.NET標識添加到Visual Studio,並且我一直在使用this page在VB中使用ASP.NET標識

問題是,該文章中的所有內容都在C#中,而我的項目特別在Visual Basic中(ASP.NET Framework 4.5.1,而不是 MVC)。另外,我對Web開發人員太陌生,無法確切知道要改變什麼。我發現了一個C#到VB轉換器,它的大部分工作,但我仍然失去了一些東西,因爲我得到了VS內的錯誤。即使我不明白爲什麼,我甚至不能100%確定我能夠使用VB代替C#工作。

我想盡可能保持簡潔,所以我將在中加上大寫字母

這是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中沒有錯誤,但在運行頁面後,我得到在瀏覽器中的「分析器錯誤」:「分析器錯誤信息:未能加載鍵入'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 

在根據「暗淡用戶」行,字鍵「」有此錯誤:

Name of field or property being initialized in an object initializer must start with '.'.

在與'行昏暗的結果 '字 '密碼' 有這樣:

'Password' is not declared. It may be inaccessible due to its protection level

上線下' result.S ucceeded」,字 'StatusMessage' 有這樣:

'StatusMessage' is not declared. It may be inaccessible due to its protection level.

最後,同一行, 'user.UserName' 有這樣:

'UserName' is not a member of 'System.Security.Principal.IPrincipal'.

我只是一直在使用VB進行大約六個星期,所以我是一個嬰兒開發。我知道如何聲明,但我不明白爲什麼上面的鏈接作者不需要在代碼中做任何額外的事情,除了它是C#。

無論如何,感謝您閱讀和提前協助。

回答

2

我能解決我的問題。步驟:

1)完全卸載VS 2013高級版
2)安裝了VS 2013終極
3)當開始一個新的項目,而不是使用一個空的模板,我選擇了「Web窗體」使用默認的「單個用戶帳戶'認證。
4)從NuGet包安裝'Microsoft ASP.NET Identity EntityFramework'

運行應用程序,然後正常工作;我能夠註冊一個用戶沒有任何錯誤。

爲了將來的參考,正確的代碼如下。請注意,'Owin'引用必須導入。

Register.aspx:

<%@ Page Title="Register" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="Register.aspx.vb" Inherits="WebFormsIdentity.Register" %> 

<%@ Import Namespace="WebFormsIdentity" %> 
<%@ Import Namespace="Microsoft.AspNet.Identity" %> 
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"> 
    <h2><%: Title %>.</h2> 
    <p class="text-danger"> 
     <asp:Literal runat="server" ID="ErrorMessage" /> 
    </p> 

    <div class="form-horizontal"> 
     <h4>Create a new account</h4> 
     <hr /> 
     <asp:ValidationSummary runat="server" CssClass="text-danger" /> 
     <div class="form-group"> 
      <asp:Label runat="server" AssociatedControlID="Email" CssClass="col-md-2 control-label">Email</asp:Label> 
      <div class="col-md-10"> 
       <asp:TextBox runat="server" ID="Email" CssClass="form-control" TextMode="Email" /> 
       <asp:RequiredFieldValidator runat="server" ControlToValidate="Email" 
        CssClass="text-danger" ErrorMessage="The email field is required." /> 
      </div> 
     </div> 
     <div class="form-group"> 
      <asp:Label runat="server" AssociatedControlID="Password" CssClass="col-md-2 control-label">Password</asp:Label> 
      <div class="col-md-10"> 
       <asp:TextBox runat="server" ID="Password" TextMode="Password" CssClass="form-control" /> 
       <asp:RequiredFieldValidator runat="server" ControlToValidate="Password" 
        CssClass="text-danger" ErrorMessage="The password field is required." /> 
      </div> 
     </div> 
     <div class="form-group"> 
      <asp:Label runat="server" AssociatedControlID="ConfirmPassword" CssClass="col-md-2 control-label">Confirm password</asp:Label> 
      <div class="col-md-10"> 
       <asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" CssClass="form-control" /> 
       <asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmPassword" 
        CssClass="text-danger" Display="Dynamic" ErrorMessage="The confirm password field is required." /> 
       <asp:CompareValidator runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" 
        CssClass="text-danger" Display="Dynamic" ErrorMessage="The password and confirmation password do not match." /> 
      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <asp:Button runat="server" OnClick="CreateUser_Click" Text="Register" CssClass="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
</asp:Content> 

Register.aspx.vb:

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 
2

好的。 首先刪除單詞「KEY」 - 它來自Telerik轉換器 - 我不知道原因。其次。在您的HTML代碼上重複ID = UserName和ID =密碼win name =「UserName」和name =「Password」 希望這會有所幫助。

+0

謝謝!我會給它一個鏡頭,讓你知道。 – Wullaj

+0

如果我能'upvote'你的答案,我會的。 「關鍵」參考絕對是一個問題。感謝您的幫助! – Wullaj