[英]How to properly return a view from controller?
I have an application with login. 我有一个具有登录名的应用程序。 I have a controller and a view.
我有一个控制器和一个视图。 From controller, I try to decide if the user is admin or not, and then to show a specific message in view.
从控制器,我尝试确定用户是否为管理员,然后在视图中显示特定消息。
The problem is that when I run the application from Startup and I press the login button, the application redirects me to the home page. 问题是,当我从“启动”运行该应用程序并按登录按钮时,该应用程序将我重定向到主页。 When I run the application from the view, it works (it's stupid, but it works).
当我从视图中运行该应用程序时,它可以工作(虽然很愚蠢,但是可以工作)。
The link when I run the app prof Startup: localhost:2627/Account/Login
当我运行应用程序教授启动时的链接:
localhost:2627/Account/Login
The link when I run the app from view: localhost:2627/Account/LoginReturnUrl=%2FUsers%2FIndex
当我从视图运行应用程序时的链接:
localhost:2627/Account/LoginReturnUrl=%2FUsers%2FIndex
This is my controller: 这是我的控制器:
public class UsersController : Controller
{
// GET: Users
[Authorize]
public ActionResult Index()
{
if (User.Identity.IsAuthenticated)
{
var user = User.Identity;
ViewBag.Name = user.Name;
ViewBag.displayMenu = "No";
if (isAdminUser())
{
ViewBag.displayMenu = "Yes";
}
return View();
}
else
{
ViewBag.Name = "Not Logged IN";
}
//return View();
return View();
}
This is my View: 这是我的观点:
@{
ViewBag.Title = "Index";
}
@if (ViewBag.displayMenu == "Yes")
{
<h1>Welcome Admin. Now you can create user Role.</h1>
<h3>
<li>@Html.ActionLink("Manage Role", "Index", "Role")</li>
</h3>
}
else
{
<h2> Welcome <strong>@ViewBag.Name</strong> :) .We will add user module soon </h2>
}
I was trying to follow this tutorial(the login part). 我试图按照本教程(登录部分)进行操作。 I can't figure out why it doesn't open my view.
我不知道为什么它不能打开我的视野。 The user is authenticated, but I only see home view.
用户已通过身份验证,但我只能看到主视图。
What am I doing wrong? 我究竟做错了什么? Do you have any suggestions?
你有什么建议吗?
Thank you! 谢谢!
The reason it's working when you start the app from the User's view is that you are trying to access a protected resource ( ie, a resource that requires authentication). 当您从用户的角度启动应用程序时,它起作用的原因是您正在尝试访问受保护的资源(即,需要身份验证的资源)。 When the user is not authenticated, the default behavior is to challenge the user ( ie, authenticate the user) and the user is redirected to a login page.
当用户未通过身份验证时,默认行为是向用户提出挑战(即,对用户进行身份验证),然后将用户重定向到登录页面。 When the user is redirected, the
returnUrl
query parameter is being set so that the user can be redirected back to this resource after authentication. 重定向用户时,将设置
returnUrl
查询参数,以便在验证后可以将用户重定向回该资源。 In this case, the returnUrl
is the User's index view. 在这种情况下,
returnUrl
是用户的索引视图。
The link when I run the app from view:localhost:2627/Account/LoginReturnUrl=%2FUsers%2FIndex
当我从视图运行应用程序时的链接:localhost:2627 / Account / LoginReturnUrl =%2FUsers%2FIndex
It sounds like you want the Users
view to be your homepage or at least the page that the user is redirected to after login. 听起来您想让
Users
视图成为您的主页,或者至少是用户登录后重定向到的页面。 In your AccountController
, you will want to force this action if this is the desired behavior. 如果这是所需的行为,则在
AccountController
,您将要强制执行此操作。
for example: 例如:
switch (result)
{
case SignInStatus.Success:
return return RedirectToAction("Index", "Users");
...
}
You will also want to beware if a returnUrl
is present too. 您还需要提防是否也存在
returnUrl
。 Based on your requirements, you might want to give the returnUrl
the highest precedence. 根据您的要求,您可能要给
returnUrl
最高的优先级。
If ( SignInStatus.Success)
{
return !string.IsNullOrEmpty(returnUrl) ?
RedirectToLocal(returnUrl):
RedirectToAction("Index", "Users");
}
Basically, it really comes down to where you want to send (aka route) the user after authentication. 基本上,它实际上取决于您要在身份验证之后将用户发送(也称为路由)的位置。 Maybe the user has a preference for their home page or that if the user is an admin that you always route them to the index view in the
UsersController
. 也许用户对他们的主页有偏好,或者如果用户是管理员,那么您总是将他们路由到
UsersController
的索引视图。
In the demo project, you should review the Start.Auth.cs file and become familiar with the authentication options. 在演示项目中,您应该查看Start.Auth.cs文件并熟悉身份验证选项。
For example, the LoginPath is being set in this file. 例如,正在此文件中设置LoginPath。 This is the path that user will be redirected to for authentication challenges.
这是用户将被重定向到身份验证挑战的路径。
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{...}
});
Reference RedirectToAction: 参考RedirectToAction:
http://msdn.microsoft.com/en-us/library/dd470594.aspx http://msdn.microsoft.com/en-us/library/dd470594.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.