I'm using asp.net web forms, coding in VB and suddenly my identity login/reg stopped working. When I'm logging in, it doesn't authenticate that it is logged in, same with registration. I have connection with the sql.
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
Thanks for any help.
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
I'd suggest you debug the code in the SignIn
method. A couple of things might be worth checking.
Put a break point on this line Dim user = userManager.Find(UserName.Text, Password.Text)
and see what value it'd return.
Check if the line LoginStatus.Visible = True
is in the right place. To me, perhaps it should have been placed outside of your If...Else...End If
block. Otherwise it'd not show any UI after the redirection to Login.aspx page. Your authentication might be ok but just you can't see any indications on the UI.
Check your web.config settings, particularly in the authentication configuration section.