c# - IsAuthenticated Staying false when cookie has been set -
i using asp.net identity core. no working ok cause can create user through registration page , appears in tables fine. issue having on login.
i including startup.cs main methods see if knows have missed when check isauthenticated attribute returning false.
// method gets called runtime. use method add services container. public void configureservices(iservicecollection services) { // add framework services. services.addmvc(); services.adddbcontext<identitydbcontext>(options => options.usesqlserver(configuration.getconnectionstring("defaultconnection"),b=>b.migrationsassembly("solitudeeccore"))); services.addidentity<identityuser, identityrole>() .addentityframeworkstores<identitydbcontext>() .adddefaulttokenproviders(); services.addtransient<imessageservice, filemessageservice>(); services.addauthentication( options => options.signinscheme = cookieauthenticationdefaults.authenticationscheme); }
i have placed @ end of configure method in startup cs.
app.usecookieauthentication(new cookieauthenticationoptions() { authenticationscheme = "cookieauthentication", loginpath = new pathstring("/account/login"), accessdeniedpath = new pathstring("/account/forbidden/"), automaticauthenticate = true, automaticchallenge = true }); app.usemvc(routes => { routes.maproute( name: "default", template: "{controller=home}/{action=index}/{id?}"); }); app.useidentity(); }
my account controller , how intiate variables.
private readonly usermanager<identityuser> _usermanager; private readonly signinmanager<identityuser> _signinmanager; private readonly imessageservice _messageservice; [httppost] public async task<iactionresult> login(string email, string password, bool rememberme) { var user = await _usermanager.findbyemailasync(email); if (user == null) { modelstate.addmodelerror(string.empty, "invalid login"); return view(); } if (!user.emailconfirmed) { modelstate.addmodelerror(string.empty, "confirm email first"); return view(); } var passwordsigninresult = await _signinmanager.passwordsigninasync(user, password, ispersistent: rememberme, lockoutonfailure: false); if (!passwordsigninresult.succeeded) { modelstate.addmodelerror(string.empty, "invalid login"); return view(); } return redirect("~/"); }
my actual login page
<form asp-controller="account" asp-action="login" method="post"> <div class="form-group has-feedback"> <input type="email" class="form-control" name="email" id="email" placeholder="email"> <span class="glyphicon glyphicon-envelope form-control-feedback"></span> </div> <div class="form-group has-feedback"> <input type="password" class="form-control" id="password" placeholder="password"> <span class="glyphicon glyphicon-lock form-control-feedback"></span> </div> <div class="row"> <div class="col-xs-8"> <div class="checkbox icheck"> <label> <input type="checkbox" name="rememberme" value="true"> remember me <input type="hidden" name="rememberme" value="false" /> </label> </div> </div> <!-- /.col --> <div class="col-xs-4"> <button type="submit" class="btn btn-primary btn-block btn-flat">sign in</button> </div> <!-- /.col --> </div> </form>
it after this variable staying false have idea.
@if (user.identity.isauthenticated) { <p>user @user.identity.name signed in. <a href="/account/logout">logout</a> </p> } else { <p>no user signed in.</p> }
edit 2 complete startup methods in quesiton after below comments , still having same result user not authenticated.
public startup(ihostingenvironment env) { var builder = new configurationbuilder() .setbasepath(env.contentrootpath) .addjsonfile("appsettings.json", optional: false, reloadonchange: true) .addjsonfile($"appsettings.{env.environmentname}.json", optional: true) .addenvironmentvariables(); configuration = builder.build(); } public iconfigurationroot configuration { get; } // method gets called runtime. use method add services container. public void configureservices(iservicecollection services) { // add framework services. services.addmvc(); services.adddbcontext<identitydbcontext>(options => options.usesqlserver(configuration.getconnectionstring("defaultconnection"),b=>b.migrationsassembly("solitudeeccore"))); services.addidentity<identityuser, identityrole>() .addentityframeworkstores<identitydbcontext>() .adddefaulttokenproviders(); services.addtransient<imessageservice, filemessageservice>(); services.addauthentication(); } // method gets called runtime. use method configure http request pipeline. public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory) { loggerfactory.addconsole(configuration.getsection("logging")); loggerfactory.adddebug(); if (env.isdevelopment()) { app.usedeveloperexceptionpage(); } else { app.useexceptionhandler("/home/error"); } app.usestaticfiles(); app.useidentity(); app.usemvc(routes => { routes.maproute( name: "default", template: "{controller=home}/{action=index}/{id?}"); }); }
Comments
Post a Comment