OWIN - 自定义UserManager

8

我需要自定义UserManager类以在公司结构中查找和验证用户(混合使用Active Directory身份验证和另一种Oracle身份验证)。虽然我已经实现了FindAsyncCreateIdentityAsync,但用户未被设置为已验证。

我的UserManager实现:

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Security.Claims;
using System.Web;
using MyProject.Common;
using MyProject.Models;
using Microsoft.AspNet.Identity;
using System.Threading.Tasks;

namespace MyProject.Infrastructure
{
    public class GNUserManager : UserManager<ApplicationUser>
    {
        public GNUserManager(IUserStore<ApplicationUser> store) : base(store)
        {

        }        

        public override async Task<ApplicationUser> FindAsync(string userName, string password)
        {
            /* Performs some logic here that returns true */

            if (foundUser) {
                return await Task.Run(() => new ApplicationUser
                {
                    UserName = userName, 
                    Id = userName
                });
            }

            throw new Exception("User not found.");
        }

        public override async Task<ClaimsIdentity> CreateIdentityAsync(ApplicationUser user, string authenticationType)
        {
            IList<Claim> claimCollection = new List<Claim>
            {
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim(ClaimTypes.Country, "Brazil"),
                new Claim(ClaimTypes.Email, user.UserName)
            };

            var claimsIdentity = new ClaimsIdentity(claimCollection, "Company Portal");

            return await Task.Run(() => claimsIdentity);  
        }
    }
}

我的用户身份验证还需要什么?
4个回答

2

尝试更改这一行。

 var claimsIdentity = new ClaimsIdentity(claimCollection, "Company Portal");

对于这个问题

var claimsIdentity = new ClaimsIdentity(claimCollection, DefaultAuthenticationTypes.ApplicationCookie);

这应该会为您生成所需的 cookie。


1

看一下ASP.NET MVC 5默认项目创建的SignIn方法,我们可以看到以下代码:

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

我们可以注意到,AuthenticationManager 是负责身份验证登录的人,在获取身份后还需要使用 AuthenticationManager 进行登录。因此,您的问题可能不在于 UserManagerController 类中的 AuthenticationManager 实例是通过以下代码检索的:
private IAuthenticationManager AuthenticationManager
{
    get
    {
        return HttpContext.GetOwinContext().Authentication;
    }
}

这行代码:AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); 正常执行,但没有生成cookie。在调试此代码时,我深入研究了 public override async Task<ClaimsIdentity> CreateIdentityAsync(ApplicationUser user, string authenticationType)。我需要找出如何生成cookie。 - Leonel Sanches da Silva
你可以调用你要重写的基本方法吗? - iuristona
我现在正在聊天中...请到那里查看。 - iuristona

1
用户管理器(UserManager)负责在数据库中管理用户身份以及验证凭据。简而言之,它是一个数据库查找工具。要让用户“登录”您的应用程序,您需要发出某种令牌(如浏览器应用程序的cookie或API应用程序的token)。最近ASP.NET的最新方法是通过Cookie身份验证中间件来处理浏览器应用程序。有关cookie中间件的更多信息,请参见此处:

http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/


我昨天看到了这个网站,但它只是我正在寻找的东西的一个概览。那正是我感兴趣的cookie生成。 - Leonel Sanches da Silva

1

目前,Oracle Data Provider for .NET 不支持异步查询和保存。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接