本文介绍了FormsAuthentication.GetRedirectUrl总是返回默认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC应用程序,并使用窗体身份验证我。当去到要求身份验证,这意味着没有对控制器操作的[授权]属性页面时,它的用户登录页面重定向与返回URL像的http://本地主机/登录RETURNURL = / MyAuthorizedUrl

I have an ASP.NET MVC app and am using Forms auth. When going to a page that requires authentication, meaning there is an [Authorize] attribute on the controller action, it redirects the user to the login page with a return url like http://localhost/Login?ReturnUrl=/MyAuthorizedUrl.

这是我的配置是如何设置:

This is how my config is setup:

<authentication mode="Forms">
  <forms loginUrl="~/Login" timeout="2880" defaultUrl="~/" />
</authentication>

这是我应得的重定向网址:

This is how I'm getting the redirect url:

var url = FormsAuthentication.GetRedirectUrl( model.Email, model.RememberMe );

这总是返回默认的网址。

This always returns the default url.

是什么原因造成的?

推荐答案

我想你想获得MyAuthorizedUrl为 FormsAuthentication.GetRedirectUrl 的结果?

I assume you would like to get "MyAuthorizedUrl" as the result of FormsAuthentication.GetRedirectUrl?

您需要插入一个隐藏的输入字段镜子 RETURNURL = / MyAuthorizedUrl ,例如 NAME =RETURNURLVALUE =/ MyAuthorizedUrl

You'll need to insert a hidden input field that mirrors ReturnUrl=/MyAuthorizedUrl, e.g. name="ReturnUrl" value="/MyAuthorizedUrl".

原因是,在登录页面通过使用GET请求的 RETURNURL ,但POST去 /登录(不带任何参数)。

The reason is that the login page is requested via GET with the ReturnUrl, but the POST goes to /Login (without any parameters).

或者改变表单action属性包含 RETURNURL 参数。

Alternatively change the form action attribute to include the ReturnUrl parameter.

这篇关于FormsAuthentication.GetRedirectUrl总是返回默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:31