TokenMismatchException in VerifyCsrfToken.php line

2019-01-20 09:16发布

When I try to login show me token error. I have checked token in view form it's right and when comment \App\Http\Middleware\VerifyCsrfToken::class, in the Kernel.php it makes me login but after Redirect to my dashboard I'm not logged in. I am using MAMP on mac.

<div>
    <h1>Login</h1>
    <div>
        {!! Form::open(['url'=>'user/login','class' => '']) !!}
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <ul>
          <li><label>Customer Code</label>{!!Form::Text('customer_code',Input::old('customer_code'),['class'=>''])!!}</li>
          <li><label>Password</label>{!!Form::Password('password','',['class'=>''])!!}</li>
          <li>{!! Form::submit('Submit',array('class' => 'btn')) !!}</li>
        </ul> 
        {!!Form::close()!!}
    </div>
    <div><a href="{!!URL::to('user/forget_password')!!}">Forget Password</a></div>
</div>

Meanwhile I use Sentry Package for login.

    /**
     * post_login
     */
    public function post_login()
    { 
        try
        {
            $rules  = [ 
                    'customer_code'         => 'required',
                    'password'              => 'required',
                ] ;                    
            $message = [ 
                    'customer_code.required'             => 'erorrr1',
                    'password.required'                =>'error2'    
                             ];                            
            $validator = Validator::make(Input::all(), $rules,$message);
            if ($validator->fails())
            {            
                return Redirect::back()->withErrors($validator)->withInput();        
            } // if ($validator->fails())
            else
            {
            $authUser = Sentry::authenticateAndRemember(array(
                                      'customer_code'    => Input::get('customer_code'),
                                      'password' => Input::get('password')), false);

                           if($authUser) 
                           {
                                //$login = Sentry::loginAndRemember($authUser);
                                 return Redirect::to('user/panel/'.$authUser->id)->with('comment', 'Welcome');
                           }
                           else
                           {
                             return Redirect::back()->with('comment', 'Error for login');
                           }
            }//validator                           
        }
         catch(\Exception $e)
         {
             return Redirect::back()->withInput(Input::except('password','file'))->withErrors(['ERROR!!!!!']);
         }
}

13条回答
孤傲高冷的网名
2楼-- · 2019-01-20 09:23

You did not post your sample code in your question.

Therefore check your code with the following options,

try with hidden input field value:

{!! csrf_token() !!} or {{ csrf_token() }}

You can also use form blade template:

{!! Form::open(array('method' => 'GET/POST','url' => 'YOUR_URL',)) !!}

This will automatically add CSRF Code in your html script

One more thing to include in <head> section is:

<meta name="csrf-token" content="{{ csrf_token() }}">
查看更多
劳资没心,怎么记你
3楼-- · 2019-01-20 09:26

It works for me.

<meta name="csrf-token" content="{{ csrf_token() }}" />

<script>
function getMessage(){ 
$.ajax({
   headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
   type:'POST',
   url:'/getmsg',
   // data:'_token = <?php echo csrf_token() ?>',
   success:function(data){
      $("#msg").html(data.msg);
   }
 });
}
</script>

{{ Form::button('Replace Message',['onClick'=>'getMessage()']) }}
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-20 09:27

This solution worked for me:

Add {{ csrf_field() }} anywhere in the form.

查看更多
放我归山
5楼-- · 2019-01-20 09:28

Adding {!! csrf_field() !!} solved my problem as shown below:

<form action="#" method="post" class="form-horizontal" role="form">
{!! csrf_field() !!}

</form>

If using Laravel Form helper such as below:

{!! Form::open(array('class' => 'form-horizontal', 'role' => 'form')) !!}

CSRF Code will be added automatically in your html script. Also make sure to view the source code in browser to be certain that a field such as below was indeed added.

<input type="hidden" name="_token" value="dHWBudjTyha9AMr0SuV2ABq5NNK6bTIDZDXRWCBA">
查看更多
你好瞎i
6楼-- · 2019-01-20 09:29

Add <?php echo Form::token(); ?> in side the form.

查看更多
Explosion°爆炸
7楼-- · 2019-01-20 09:33

I used the following code. It is working perfectly.

<?php echo csrf_token(); ?>
查看更多
登录 后发表回答