2017-03-16 77 views
1

我使用的是asp.net web表单,在VB中编码,突然我的身份登录/ reg停止工作。当我登录时,它不会验证它是否已登录,与注册一样。我有与SQL连接。asp净身份 - 登录将无法正确验证

的Login.aspx

<asp:PlaceHolder runat="server" ID="LoginStatus" Visible="false"> 
     <p> 
      <asp:Literal runat="server" ID="StatusText" /> 
     </p> 
       </asp:PlaceHolder> 
       <asp:PlaceHolder runat="server" ID="LoginForm" Visible="false"> 
       <div class="form-group"> 
        <asp:Label runat="server" AssociatedControlID="UserName" CssClass="col-md-2 control-label">User name</asp:Label> 
        <div class="col-md-6"> 
        <asp:TextBox runat="server" ID="UserName" CssClass="form-control" /> 
        </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-6"> 
        <asp:TextBox runat="server" ID="Password" TextMode="Password" CssClass="form-control" /> 
        </div> 
       </div> 
       <div class="form-group"> 
        <div class="col-md-10 col-md-offset-2"> 
        <asp:Button runat="server" Text="Log in" OnClick="SignIn" CssClass="btn btn-default" /> 
        </div> 
       </div> 
        </asp:PlaceHolder> 
       <asp:PlaceHolder runat="server" ID="LogoutButton" Visible="false"> 
     <div> 
      <div> 
       <asp:Button runat="server" OnClick="SignOut" Text="Log out" CssClass="btn btn-default" /> 
      </div> 
     </div> 
    </asp:PlaceHolder> 

Login.aspx.vb

Imports System.Net 
Imports System.Web  
Imports System.Web.UI 
Imports Microsoft.AspNet.Identity 
Imports Microsoft.AspNet.Identity.EntityFramework 
Imports Microsoft.AspNet.Identity.Owin 
Imports Microsoft.Owin.Security 
Imports Owin 

Partial Public Class Login 
    Inherits Page 
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
     If Not IsPostBack Then 
      If User.Identity.IsAuthenticated Then 
       StatusText.Text = String.Format("Logged in as {0}", User.Identity.GetUserName()) 
       LoginStatus.Visible = True 
       LogoutButton.Visible = True 
      Else 
       LoginForm.Visible = True 
      End If 
     End If 
    End Sub 

    Protected Sub SignIn(sender As Object, e As EventArgs) 
     Dim userStore = New UserStore(Of IdentityUser)() 
     Dim userManager = New UserManager(Of IdentityUser)(userStore) 
     Dim user = userManager.Find(UserName.Text, Password.Text) 

     If user IsNot Nothing Then 
      Dim authenticationManager = HttpContext.Current.GetOwinContext().Authentication 
      Dim userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie) 

      authenticationManager.SignIn(New AuthenticationProperties() With { 
       .IsPersistent = False 
      }, userIdentity) 
      Response.Redirect("Login.aspx") 
     Else 
      StatusText.Text = "Invalid username or password." 
      LoginStatus.Visible = True 
     End If 
    End Sub 
    Protected Sub SignOut(sender As Object, e As EventArgs) 
     Dim authenticationManager = HttpContext.Current.GetOwinContext().Authentication 
     authenticationManager.SignOut() 
     Response.Redirect("~/Home.aspx") 
    End Sub 


End Class 

感谢您的帮助。

+1

请提供您得到 –

+0

这就是事物的错误信息,我没有得到一个错误,它甫一通过authenticationManager.SignIn运行,并返回null,则重定向 –

+0

我不知道是什么原因造成这,不过你可以试试这个。(C#)'如果(User.Identity.IsAuthenticated) {// 使用已经登录 } 其他 {VAR 用户= userManager.Find(UserName.Text,Password.Text) //其余的东西 } ' –

回答

1
Protected Sub SignIn(sender As Object, e As EventArgs) 
    Dim userStore = New UserStore(Of IdentityUser)() 
    Dim userManager = New UserManager(Of IdentityUser)(userStore) 
    Dim user = userManager.Find(UserName.Text, Password.Text) 

    If user IsNot Nothing Then 
     Dim authenticationManager = HttpContext.Current.GetOwinContext().Authentication 
     Dim userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie) 

     authenticationManager.SignIn(New AuthenticationProperties() With { 
      .IsPersistent = False 
     }, userIdentity) 
     Response.Redirect("Login.aspx") 
    Else 
     StatusText.Text = "Invalid username or password." 
     LoginStatus.Visible = True 
    End If 
End Sub 

我建议你调试SignIn方法中的代码。一些事情可能值得检查。

  1. 在这条线上放一个断点Dim user = userManager.Find(UserName.Text, Password.Text),看看它会返回什么值。

  2. 检查行LoginStatus.Visible = True是否在正确的位置。对我来说,也许它应该放在你的If...Else...End If区块之外。否则,重定向到Login.aspx页面后不会显示任何UI。您的身份验证可能没问题,但只是在用户界面上看不到任何指示。

  3. 检查您的web.config设置,特别是在authentication配置部分。

+0

谢谢!更正了webconfig中的认证 –