簡體   English   中英

登錄后根據那里的角色重定向到特定區域的用戶

[英]Redirect to user on specific area after login based on there role

我正在使用 dot.net core 6.0 創建 web 應用程序。 應用程序包含兩種類型的用戶,一種是管理員,第二種是簡單用戶區域。 我希望當用戶基於那里的角色登錄時,他/她重定向到那里的區域,就像如果用戶是管理員,他重定向到管理區域。 否則重定向到用戶區域。 `

@if (signInManager.IsSignedIn(User))
 {
 if (User.IsInRole("SuperAdmin"))
 {
 Response.Redirect(Url.Action("Index", "AdminDashboard", new { Area = "SuperAdmin" }));
}
else if (User.IsInRole("User"))
{
Response.Redirect(Url.Action("Index", "User", new { Area = "User" }));
}
}

` 我在布局文件中嘗試此代碼,但此代碼無法正常工作

試試這個方法

@if (signInManager.IsSignedIn(User))
{
var user =await    userManager.FindByNameAsync(Input.Email);
var roles = await  userManager.GetRolesAsync(user);
if (roles.Contains("SuperAdmin"))
 {
 Response.Redirect(Url.Action("Index", "AdminDashboard", new { Area = 
 "SuperAdmin" }));
 }
}

我認為這可以幫助你!

暫無
暫無

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

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