简体   繁体   English

FormAuthentication重定向到不同的登录页面C#

[英]FormAuthentication Redirect to Different login page C#

I am creating an ASP.NET webform application where I have a different level of users. 我正在创建一个ASP.NET webform应用程序,我有不同级别的用户。 I have read about FormsAuthentication and if I am implementing it, I have to provide a loginUrl in web.config file. 我已阅读有关FormsAuthentication ,如果我正在实现它,我必须在web.config文件中提供loginUrl Everyone is redirected to the same loginUrl after successfull authentication. 成功验证后,每个人都被重定向到相同的loginUrl。

But For my site, I have users with different credentials. 但对于我的网站,我有不同凭据的用户。 So when a user with Admin credential is login, system redirect it to Mysite.com/Admin/Index.aspx page. 因此,当具有Admin凭据的用户登录时,系统会将其重定向到Mysite.com/Admin/Index.aspx页面。 IF a user is login with Student credential, he is redirect it to Mysite.com/Student/index.aspx page. 如果用户使用Student凭证登录,则会将其重定向到Mysite.com/Student/index.aspx页面。

Can anyone please guide me or suggest me how to implement this? 任何人都可以指导我或建议我如何实现这一点?

Thanks 谢谢

As per @elolos comment, all you have to do is Redirect them to the correct page after you have logged them in. 根据@elolos评论,您所要做的就是在登录后将它们重定向到正确的页面。

For example 例如

public ActionResult Login(LoginModel model){
   if (!(ModelState.IsValid) || !(Membership.ValidateUser(model.username, model.password))){
     // handle error
   }

   .. set session variables, cookies ,etc ..

   if (User.IsInRole("Admin")){
      return Redirect(Url.Action("Index","Admin"))
   }
   if (User.IsInRole("Student")){
      return Redirect(Url.Action("Index","Student"))
   }

   ... and so on ...
}

This may help https://social.technet.microsoft.com/wiki/contents/articles/19740.forms-authentication-with-multiple-login-pages.aspx 这可能有助于https://social.technet.microsoft.com/wiki/contents/articles/19740.forms-authentication-with-multiple-login-pages.aspx

But imho if you have different login pages you need to check redirect URL or if you don't have redirect URL you need to check refferer URL to show correct login page. 但是如果您有不同的登录页面需要检查重定向URL,或者如果您没有重定向URL,则需要检查refferer URL以显示正确的登录页面。 Otherwise for session timeouts your app will be ended by showing wrong login page. 否则,对于会话超时,您的应用将通过显示错误的登录页面而结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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