首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC5身份验证无法保存UseCookieAuthentication

MVC5身份验证无法保存UseCookieAuthentication
EN

Stack Overflow用户
提问于 2015-09-10 04:37:20
回答 1查看 158关注 0票数 0

在成功登录后,我对MVC 5身份验证有问题,我认为我无法保存UseCookieAuthentication。

  • 我的启动代码:

public partial class Startup { public void Configuration(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/MyAccount/Login") }); // Use a cookie to temporarily store information about a user logging in with a third party login provider app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); } }

  • 我的登入者: 公共a.Password.Equals(l.Password)).FirstOrDefault();登录(Login l,string ReturnUrl="") { user (CBDB2012Entities dc =新CBDB2012Entities()) { var user= dc.Users.Where(a => a.UserName.Equals(l.Username) & ActionResult if (user != null) { FormsAuthentication.SetAuthCookie(user.UserName,l.RememberMe);如果(Url.IsLocalUrl(ReturnUrl)) {返回重定向(ReturnUrl);}RedirectToAction{返回RedirectToAction("MyProfile","MyAccount");}}ModelState.Remove(“密码”);返回视图();}

登录后,it RedirectToAction(" MyProfile ","MyAccount");但在MyProfile视图中,我看不到任何关于用户的信息,我无法使用授权访问contac页面

-Contact控制器

[Authorize] public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); }

EN

回答 1

Stack Overflow用户

发布于 2015-09-10 06:21:44

我在登录控制中通过会话进行了测试,它运行得很好:

代码语言:javascript
复制
var user = dc.Users.Where(a => a.UserName.Equals(l.Username) && a.Password.Equals(l.Password)).FirstOrDefault();
            if (user != null)
            {
                FormsAuthentication.SetAuthCookie(user.UserName, l.RememberMe);
                Session["loginname"] = user.Employee.FirstName;

                if (Url.IsLocalUrl(ReturnUrl))
                {
                    return Redirect(ReturnUrl);
                }
                else
                {
                    return RedirectToAction("MyProfile", "MyAccount");
                }
            }

我可以从数据库获取视图中的会话“loginname”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32493253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档