首页 新闻 会员 周边

MVC 2.0框架中 怎样实现 Forms 身份验证

1
悬赏园豆:20 [待解决问题]

 有这样一个场景

在登录页面中 LogOn.aspx提供用户登录。

登录成功转到  Index.aspx,如果用户没有登录 则不能直接访问Index.aspx

我早web.config 这样配置:

    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" defaultUrl="~/User/Index" protection="All"/>
    </authentication>

然后在 控制器里 Index()方法前  放[Authorize]

我在登录的控制器里  LogOn()  怎样写代码  实现该功能

 

菜鸟学敲代码的主页 菜鸟学敲代码 | 初学一级 | 园豆:5
提问于:2010-08-19 14:46
< >
分享
所有回答(3)
0
public IFormsAuthenticationService FormsService { get; set; }
public IMembershipService MembershipService { get; set; }

protected override void Initialize(RequestContext requestContext)
{
if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

base.Initialize(requestContext);
}


// **************************************
// URL: /Account/LogOn
// **************************************

public ActionResult LogOn()
{
return View();
}

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
FormsService.SignIn(model.UserName, model.RememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Account");
}
}
else
{
ModelState.AddModelError(
"", "The user name or password provided is incorrect.");
}
}

// If we got this far, something failed, redisplay form
return View(model);
}

// **************************************
// URL: /Account/LogOff
// **************************************

public ActionResult LogOff()
{
FormsService.SignOut();

return RedirectToAction("Index", "Home");
}

 

mvc自动生成代码中就有。

Launcher | 园豆:45045 (高人七级) | 2010-08-19 14:50
嗯,我还有个问题 就是登录后,在头部 显示 欢迎 XXX 之类的 那个名字我在哪个地方取出来显示 然后 链接 变成 退出
支持(0) 反对(0) 菜鸟学敲代码 | 园豆:5 (初学一级) | 2010-08-19 14:55
@菜鸟学敲代码:大哥,你用MVC向导创建一个项目后,你说的这些它全都有了。Shared目录下 site.master 中 <div id="logindisplay"> <% Html.RenderPartial("LogOnUserControl"); %> </div> 然后在LogOnUserControl.ascx中: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <% if (Request.IsAuthenticated) { %> Welcome <b><%= Html.Encode(Page.User.Identity.Name) %></b>! [ <%= Html.ActionLink("我的账户", "Index", "Account") %> ] [ <%= Html.ActionLink("安全退出", "LogOff", "Account") %> ] <% } else { %> [ <%= Html.ActionLink("登录", "LogOn", "Account") %> ] [ <%= Html.ActionLink("注册", "Register", "Account")%> ] <% } %>
支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2010-08-19 14:58
@Galactica:额,大哥,谢了,俺是小弟,大哥不敢当。 我先去瞧几眼 然后再敲几行试试……
支持(0) 反对(0) 菜鸟学敲代码 | 园豆:5 (初学一级) | 2010-08-19 16:05
0

web.config设置了就可以吧

LogOn写你自己登陆的逻辑就可以了

jowo | 园豆:2834 (老鸟四级) | 2010-08-19 14:55
能不能提供一个自己写的方法啊,微软自带的那个和我要用的不一样啊
支持(0) 反对(0) 菜鸟学敲代码 | 园豆:5 (初学一级) | 2010-08-19 15:01
可以<%= Html.Encode(Page.User.Identity.Name) %>,链接 退出先在页面上写死就可以了,退出链接href="/Account/LogOff"
支持(0) 反对(0) jowo | 园豆:2834 (老鸟四级) | 2010-08-19 15:01
你自己写好一个存储过程来判断是否合法用法 然后写好类调用这个存储过程 public ActionResult LogOn(string userName,string pwd) { Login(userName, pwd); }
支持(0) 反对(0) jowo | 园豆:2834 (老鸟四级) | 2010-08-19 15:12
谢谢!我先去看看
支持(0) 反对(0) 菜鸟学敲代码 | 园豆:5 (初学一级) | 2010-08-19 16:04
@jowo:谢谢!
支持(0) 反对(0) 菜鸟学敲代码 | 园豆:5 (初学一级) | 2010-08-19 16:06
0
HUHU慈悲 | 园豆:9973 (大侠五级) | 2010-08-19 17:08
谢谢,但我要的是用写代码的
支持(0) 反对(0) 菜鸟学敲代码 | 园豆:5 (初学一级) | 2010-08-19 17:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册