加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

.net – 为什么OWIN的SignInAsync()没有设置IsAuthenticated(或

发布时间:2020-12-16 03:16:40 所属栏目:asp.Net 来源:网络整理
导读:我有一个.NET,MVC 5,EF 6项目.我们正在连接到外部项目/数据库来验证用户,因此我们需要实现的只是登录和注销方法,而不是普通的CRUD方法.我知道我已经将EF正确映射到外部数据库的表中;我可以看到它得到了用户. 我正在尝试使用OWIN进行身份验证,以便我可以在整
我有一个.NET,MVC 5,EF 6项目.我们正在连接到外部项目/数据库来验证用户,因此我们需要实现的只是登录和注销方法,而不是普通的CRUD方法.我知道我已经将EF正确映射到外部数据库的表中;我可以看到它得到了用户.

我正在尝试使用OWIN进行身份验证,以便我可以在整个控制器方法中使用[Authorize]等属性.我一直关注this作为我的向导.

在Startup.Auth.cs我有:

app.CreatePerOwinContext<CustomUserManager>(CustomUserManager.Create);
    app.CreatePerOwinContext<CustomSignInManager>(CustomSignInManager.Create);
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,LoginPath = new PathString("/Account/Login"),Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager,ApplicationUser>(
                validateInterval: TimeSpan.FromMinutes(30),regenerateIdentity: (manager,user) => user.GenerateUserIdentityAsync(manager)
            )
        },CookieSecure = CookieSecureOption.Always
    });

我还有实现接口IUserStore< User>,UserManager< User>和SignInManager< User,string>的类.我的CustomUserStore实现了以下方法,因为源代码说它是如何登录的.我为其他函数(即DeleteAsync(),UpdateAsync()等)实现了虚拟方法,这些方法不需要实现,因为我们对此外部用户db具有只读访问权限…

public async Task<User> FindByIdAsync(string id)
    {
        using (ExternalDBContext context = new ExternalDBContext())
        {
            return await Task.Run(() =>
                context.Users.Where(u => u.Id == id).ToList().FirstOrDefault()
            );
        }
    }

然后在我的AccountController.Login()我有:

await CustomSignInManager.SignInAsync(model,true,true);
        return RedirectToAction("Index","Dashboard");

但是在SignInAsync()之后,User.Identity.IsAuthenticated仍然是false,因此没有任何授权属性不起作用.

#1可能的问题:我想知道问题是否与AuthenticationManager有关,SignInManager依赖它.以下是我在Startup.Auth.cs中实现它的方法:

public class CustomSignInManager : SignInManager<User,string>
{
    public CustomSignInManager(CustomUserManager userManager,IAuthenticationManager authenticationManager)
        : base(userManager,authenticationManager)
    {
    }
    public static CustomSignInManager Create(IdentityFactoryOptions<CustomSignInManager> options,IOwinContext context)
    {
        return new CustomSignInManager(context.GetUserManager<CustomUserManager>(),context.Authentication);
    }
}

它从上下文中获取Microsoft.Owin.Security.AuthenticationManager.

那么:我是否需要做其他事情来实现身份验证?

#2可能的问题:我注意到我的CustomUserManager有一个属性SupportsUserLogin = false. That property需要实现IUserLoginStore.我不想实现另一个无用的界面,其中包含我不使用的方法!

那么:如果SupportsUserLogin = false,这有关系吗?我需要实现该接口还是有办法解决这个问题?

总结:总的来说,对于一个我想要做的就是登录用户的项目来说,这已经太复杂了.然后出去我不想创建/更新/删除用户.我只想登录用户并让User.Identity.IsAuthenticated = true直到他们退出.因此,任何关于如何回溯到更简单的使用Identity框架的方法的想法都将受到极大的赞赏.我最大的问题是我是否会走正路.

编辑:
我绝对不想做#2(实现IUserLoginStore)because that is for doing external authentication.

解决方法

你在用https吗?
如果不删除此行:CookieSecure = CookieSecureOption.Always

也没有必要IUserLoginStore(仅适用于像facebook这样的外部身份验证)
关于这一点的更多信息:here

边注:
将Task.Factory.StartNew更改为Task.Run link

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读