我需要自定义UserManager
类以在公司结构中查找和验证用户(混合使用Active Directory身份验证和另一种Oracle身份验证)。虽然我已经实现了FindAsync
和CreateIdentityAsync
,但用户未被设置为已验证。
我的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);
}
}
}
我的用户身份验证还需要什么?
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
正常执行,但没有生成cookie。在调试此代码时,我深入研究了public override async Task<ClaimsIdentity> CreateIdentityAsync(ApplicationUser user, string authenticationType)
。我需要找出如何生成cookie。 - Leonel Sanches da Silva