Jak dostosować uwierzytelnianie do własnego zestawu tabel w asp.net web api 2?
W domyślnym AccountController utworzonym widzę
public AccountController()
: this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat)
{
}
In Startup.Auth.cs I see
UserManagerFactory = () =>
new UserManager<IdentityUser>(new UserStore<IdentityUser>());
Wydaje się, że implementacja UserStore pochodzi od Microsoftu.AspNet.Tożsamość.EntityFramework .
Więc, aby dostosować uwierzytelnianie muszę zaimplementować własną wersję UserStore jak
class MYSTUFFUserStore<IdentityUser> : UserStore<IdentityUser>
{
}
I nadpisać metody, a następnie zrobić to podczas uruchamiania.Auth.cs
UserManagerFactory = () =>
new UserManager<IdentityUser>(new MYSTUFFUserStore<IdentityUser>());
Szukam poprawnego sposobu na dostosowanie uwierzytelnianie.
1 answers
Zakładając, że Twoja tabela nazywa się AppUser
, przekonwertuj swój obiekt domeny AppUser
na IUser(using Microsoft.AspNet.Identity)
w ten sposób
using Microsoft.AspNet.Identity;
public class AppUser : IUser
{
//Existing database fields
public long AppUserId { get; set; }
public string AppUserName { get; set; }
public string AppPassword { get; set; }
public AppUser()
{
this.Id = Guid.NewGuid().ToString();
}
[Ignore]
public virtual string Id { get; set; }
[Ignore]
public string UserName
{
get
{
return AppUserName;
}
set
{
AppUserName = value;
}
}
}
Zaimplementuj UserStore
obiekt w ten sposób
using Microsoft.AspNet.Identity;
public class UserStoreService
: IUserStore<AppUser>, IUserPasswordStore<AppUser>
{
CompanyDbContext context = new CompanyDbContext();
public Task CreateAsync(AppUser user)
{
throw new NotImplementedException();
}
public Task DeleteAsync(AppUser user)
{
throw new NotImplementedException();
}
public Task<AppUser> FindByIdAsync(string userId)
{
throw new NotImplementedException();
}
public Task<AppUser> FindByNameAsync(string userName)
{
Task<AppUser> task = context.AppUsers.Where(
apu => apu.AppUserName == userName)
.FirstOrDefaultAsync();
return task;
}
public Task UpdateAsync(AppUser user)
{
throw new NotImplementedException();
}
public void Dispose()
{
context.Dispose();
}
public Task<string> GetPasswordHashAsync(AppUser user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
return Task.FromResult(user.AppPassword);
}
public Task<bool> HasPasswordAsync(AppUser user)
{
return Task.FromResult(user.AppPassword != null);
}
public Task SetPasswordHashAsync(AppUser user, string passwordHash)
{
throw new NotImplementedException();
}
}
Jeśli masz własne hashowanie niestandardowego hasła, musisz również zaimplementować IPasswordHasher
. Poniżej przykład, w którym nie ma hashowania hasła(o nie!)
using Microsoft.AspNet.Identity;
public class MyPasswordHasher : IPasswordHasher
{
public string HashPassword(string password)
{
return password;
}
public PasswordVerificationResult VerifyHashedPassword
(string hashedPassword, string providedPassword)
{
if (hashedPassword == HashPassword(providedPassword))
return PasswordVerificationResult.Success;
else
return PasswordVerificationResult.Failed;
}
}
W Startupie.Auth.CS replace
UserManagerFactory = () =>
new UserManager<IdentityUser>(new UserStore<IdentityUser>());
Z
UserManagerFactory = () =>
new UserManager<AppUser>(new UserStoreService()) { PasswordHasher = new MyPasswordHasher() };
W ApplicationOAuthProvider.cs
zastąp IdentityUser
na AppUser
W AccountController.cs
, zastąp IdentityUser
z AppUser
i usuń wszystkie zewnętrzne metody uwierzytelniania, takie jak GetManageInfo
i RegisterExternal
itp.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-17 21:15:13