微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 在PasswordSignInAsync成功后,User.Identity.IsAuthenticated始终为false

我有一个标准的MVC项目,包括UserManager和SignInManager对象以及一个AccountController,具有预先创建的登录注册类型功能.

我可以将新用户注册到我的AspNetUsers表,但是当我登录时我打电话: –

var result = await SignInManager.PasswordSignInAsync(model.Email,model.Password,model.RememberMe,shouldLockout: false);

数据正确地来自表单,结果是成功,这是我所期望的.

然后我尝试了以下重定向: –

case SignInStatus.Success:
    //return RedirectToLocal("/admin/");
    return RedirectToAction("Index","Admin");

但在任何页面上,成功登录后,User.Identity.IsAuthenticated始终为false,User.Identity.Name为空字符串.

我究竟做错了什么?我以同样的方式完成了另一个项目,过去使用相同的设置,我没有遇到任何问题.

web.config

<system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
    <!--<authentication mode="Forms">
        <forms loginUrl="~/Account/Login/" timeout="1000" />
    </authentication>-->
    <authentication mode="None" />
</system.web>
<modules>
    <remove name="FormsAuthentication" />
</modules>

任何人都可以建议我做错了什么?它现在正在引发重大问题.

干杯!

解决方法

检查项目中的App_Start文件夹中是否有Startup.Auth.cs文件.
public partial class Startup {
    public void ConfigureAuth(IAppBuilder app) {            
        // This uses cookie to store information for the signed in user
        var authOptions = new CookieAuthenticationoptions {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,LoginPath = new PathString("/Account/Login"),logoutPath = new PathString("/Account/logout"),ExpireTimeSpan = TimeSpan.FromDays(7),};
        app.UseCookieAuthentication(authOptions);
    }
}

并从Startup类调用

public partial class Startup {
    public void Configuration(IAppBuilder app) {
        // Surface Identity provider
        ConfigureAuth(app);

        //..other start up code
    }
}

根据您使用的asp.net版本和身份,您应该看看这个

ASP.NET Identity AuthenticationManager vs. SignInManager and cookie expiration

原文地址:https://www.jb51.cc/aspnet/247446.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐