简体   繁体   English

根据ASP.NET MVC 4 Internet应用程序登录中的用户角色重定向到其他页面

[英]redirect to different pages based on user role in ASP.NET MVC 4 Internet Application Login

I'm creating ASP.NET MVC 4 Internet Application. 我正在创建ASP.NET MVC 4 Internet应用程序。 In that Application I created Login Page that any user can log, then I'm Trying to redirect user to different pages based on their role. 在该应用程序中,我创建了任何用户都可以登录的登录页面,然后我试图根据用户的角色将其重定向到其他页面。

ASP.NET Identity is the membership system here. ASP.NET Identity是这里的成员资格系统。

In my AspNetRoles Table I have two roles: 在我的AspNetRoles表中,我有两个角色:

Id| Name

1 | HEI_Admin

2 | HEI_User

This is my Login Controller method: 这是我的登录控制器方法:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.UserName, model.Password); 

            if (user != null)
            {
                if (user.ConfirmedEmail == true)
                {

                    await SignInAsync(user, model.RememberMe);

                    if (String.IsNullOrEmpty(returnUrl))
                    {
                        if (UserManager.IsInRole(user.Id, "HEC_Admin"))
                        {
                            return RedirectToAction("Index", "HEC");
                        }
                        //role Admin go to Admin page
                        if (UserManager.IsInRole(user.Id, "HEI_User"))
                        {
                            return RedirectToAction("Index", "HEI");
                        }
                    }

                    else
                    {
                        return RedirectToLocal(returnUrl);
                    }


                }
                else
                {
                    ModelState.AddModelError("", "Confirm Email Address.");
                }
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

but when I try to log using correct credentials I'm getting following error: 但是当我尝试使用正确的凭据登录时,出现以下错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。

All the connection are double checked, how can achieve this problem. 所有连接都经过仔细检查,如何才能解决这个问题。

        if (ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.UserName, model.Password);
            if (user != null)
            {
                await SignInAsync(user, model.RememberMe);
                if (UserManager.IsInRole(user.Id, "Admin"))
                {
                    return RedirectToAction("Index", "Admin");
                }
                else if (UserManager.IsInRole(user.Id, "Clerk"))
                {
                    return RedirectToAction("Index","Employee");
                }
                else if (UserManager.IsInRole(user.Id, "Customer"))
                {
                    return RedirectToAction("Index", "Customer");
                }
                //return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("","Invalid username or password");
            }
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

This is the code i used for redirect. 这是我用于重定向的代码。 also see if the userID and RoleID are inserted into the ASPNETUserRoles table 还查看是否将userID和RoleID插入到ASPNETUserRoles表中

if you are using any centralized database then Make sure your database engine is configured to accept remote connections. 如果您使用的是任何集中式数据库,请确保将数据库引擎配置为接受远程连接。

Go through this link i will solve the same issue with the help of this link 通过此链接,我将在此链接的帮助下解决相同的问题

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

相关问题 如何在 .ASP.NET Core MVC 中使用多个登录页面和未经授权的用户重定向不同的登录页面 - How to use multiple login pages in .ASP.NET Core MVC and unauthorized user redirect different login page 如何根据超时时的用户类型将用户重定向到 ASP.net MVC 应用程序中的不同 url - How to redirect user to different url in ASP.net MVC application based on the user type on timeout Asp.net Identity 2.0-根据登录时的角色重定向外部认证的用户 - Asp.net Identity 2.0 - redirect externally authenticated user based on role at login 登录后,用户根据asp.net中的角色重定向其他页面 - After Login User redirect different Page according to Role In asp.net ASP.NET MVC 基于用户角色显示 HTML - ASP.NET MVC Display HTML Based on User Role ASP.NET MVC或者渲染EditorFor基于用户角色 - ASP.NET MVC Alternatively Rendering EditorFor Based on User Role 基于角色的安全性asp.net mvc - Role based security asp.net mvc 基于角色的Asp.net MVC应用 - Role Based Asp.net MVC App Asp.net基于mvc角色的身份验证 - Asp.net mvc role based authentication 如何重定向我的mvc asp.net Web应用程序中除一个角色以外的所有页面? - How to redirect all pages in my mvc asp.net web app except for one role?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM