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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....