簡體   English   中英

在Manager.SignInAsync之后-重定向到另一個頁面導致注銷

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

我已為除Index之外的所有頁面添加了登錄要求。

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

您可以在Query ....?UserName=Whatever使用用戶名進入我的索引頁

我僅通過用戶名登錄用戶。 登錄成功后,我將重定向到另一個頁面,但是需要用戶名和密碼(盡管登錄成功!)。

Index.cshtml上的代碼:

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

您需要在啟動Configure方法中添加app.UseAuthentication()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM