2017-03-16 23 views
1

私はasp.net Webフォームを使用しています。これはVBでコーディングしていて、突然IDログイン/ regが機能しなくなりました。私がログインしているとき、ログインしていることを認証しません。登録と同じです。私はSQLとの接続を持っています。任意の助けasp net identity - ログインが正しく認証されない

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

を取得しているエラーメッセージを入力してください、私はエラーを取得していないよ、それはjusts authenticationManager.SignInを通ると、nullを返し、その後、私はこれを引き起こしているのかわからないのです –

+0

をリダイレクト他 { VARユーザー= userManager.Find(UserName.Text、Password.Text)、しかし、あなたはこれを試すことができます。(C#の) 'の場合(User.Identity.IsAuthenticated) {すでに ログイン //使用} //あなたの残りの部分 } ' –

答えて

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が表示されません。あなたの認証は大丈夫かもしれませんが、あなたはUI上に何も表示されません。

  3. 特に、authentication設定セクションでweb.configの設定を確認してください。

+0

ありがとう!! webconfigで訂正された認証 –

関連する問題