使用MVC3 razor,我想将firstlogin上的用户重定向到ClubController。我正在使用默认的成员资格提供程序和配置文件提供程序我在注册期间将boolean isNewUser设置为true。代码如下,但似乎逻辑不合适。我是新手,需要帮助。
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
var userProfile = ProfileBase.Create(model.UserName);
var FirstTime = userProfile.GetPropertyValue("isNewUser");
if (FirstTime == true)
{
return RedirectToAction("Create", "Club");
}
else
{
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
// If we got this far, something failed, redisplay form
return View(model);
}
我已按照How to track first login of user in MVC3?的步骤进行操作。任何帮助将非常感谢
答案 0 :(得分:0)
在模型状态行
之前和之后取出{和}答案 1 :(得分:0)
你确定FirstTime是一个布尔值而不是一个字符串吗?您可以更改if语句以确保
if(Convert.ToBoolean(FirstTime))....
答案 2 :(得分:0)
最后,这就是我设法让它解决的问题。我按如下所示制作了登录模型
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
var profile = ProfileBase.Create(model.UserName);
var FirstTime = profile.GetPropertyValue("isNewUser");
var chk=FirstTime;
if (chk.Equals(true))
return RedirectToAction("Create","Club");
else
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
return Redirect(returnUrl);
else
return RedirectToAction("Index", "Home");
}
else
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
并将配置文件设置如下
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
<properties>
<add name="isNewUser" type="System.Boolean"/>
</properties>
</profile>