如何根据用户在Asp.net C#中的角色将用户从登录页面重定向到下一页 [英] how to redirect user from login page to next page base on their Role in Asp.net C#

查看:52
本文介绍了如何根据用户在Asp.net C#中的角色将用户从登录页面重定向到下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上使用内置的asp.net角色和成员资格提供程序.

I am using built-in asp.net Role and membership provider in my website.

当用户登录到我的系统时,他们将重定向到hompage.

when user login to my system they are redirected to hompage.

我应该如何编码.当他单击登录"按钮页面时,请检查其角色,然后决定将其重定向到何处.假设用户名为John且"john"的用户登录名是"Admin",则应用程序将其重定向到AdminPanel.aspx,如果john用户是正常的"RegUser"角色,则将其重定向到Home.aspx.

how should i code it. that when he click on login button page check its role and then decide where to redirect. Suppose user login with name John and "john" is "Admin" then application Redirect him to AdminPanel.aspx and if User john is normal "RegUser" Role then redirect him into Home.aspx.

在此先感谢........

Thanks In advance........

推荐答案

在登录页面上,如果用户成功登录,则可以将其角色存储在Session和 Home.aspx 只是有一些逻辑来确定是否需要将其重定向到此页面之外,例如

On your login page, if the user was successfully logged in, you could store their role in the Session and in your Home.aspx just have a bit of logic which determines whether they need to be re-directed away from this page or not e.g.

login.aspx

void LoginBtn_Click(Object sender, EventArgs e)
{
    // have a function which returns the user object if successful, otherwise return null
    User user = DoLogin(txtUsername.Text, txtPassword.Text);
    if (user != null)
    {
        Session["UserRole"] = user.RoleName;
        // if you aren't using the authentication stuff from the web.config then
        // then you will need to manually redirect the user
    }   
}

home.aspx

void Page_Load(Object sender, EventArgs e)
{
    string role = !String.IsNullOrEmpty(Session["UserRole"]) ? (string)Session["UserRole"] : String.Empty;
    if (role == "ADMIN")
        Response.Redirect("adminpanel.aspx");
}

这篇关于如何根据用户在Asp.net C#中的角色将用户从登录页面重定向到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆