简体   繁体   中英

After Manager.SignInAsync - redirect to another page caused log out

I've added a login requirement to all pages except Index .

services.AddMvc()
        .AddRazorPagesOptions(options =>
        {
            options.Conventions.AuthorizeFolder("/");
            options.Conventions.AllowAnonymousToPage("/Index");
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

You can come to my index page with a username in Query ....?UserName=Whatever

I login the user by the username only. After loggin successful, I redirect to another page, but the user name and password are required (although the login succeeded!).

The code at Index.cshtml:

@{
   var user = 
   Manager.UserManager.FindByNameAsync(UserName).GetAwaiter().GetResult();
   Manager.SignInAsync(user, true).GetAwaiter().GetResult();
   if (Manager.IsSignedIn(user))
   {
        Response.Redirect(anotherPage, true);
   }
}

You need to add app.UseAuthentication() in startup Configure method:

app.UseAuthentication();
app.UseMvc(); // Order here is important.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM