我有一個ASP.NET Web API應用程序,它有兩種類型的用戶 - 客戶端和驅動程序。目前,我在客戶端和驅動程序控制器中有註冊客戶端和驅動程序的方法。我意識到正確的做法是通過內置的認證系統通過ApplicationUser
類。基本上,整個事情讓我感到困惑,因爲對於這兩種用戶類型我都有不同的領域。無論如何,我已經提出了兩種可能的解決方案,但是,這兩種解決方案聽起來都不像實現我的目標的正確方法。ASP.NET Web API正確的方式有兩種類型的ApplicationUser
1)繼承ApplicationUser
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
[Column(TypeName = "datetime2")]
public DateTime RegistrationDate { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
{
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
return userIdentity;
}
}
Client.cs
public class Client : ApplicationUser
{
}
Driver.cs
public class Driver: ApplicationUser
{
private ICollection<Country> countriesOfOperation;
public Driver()
{
this.countriesOfOperation = new HashSet<Country>();
}
[Column(TypeName = "datetime2")]
public DateTime DateOfBirth { get; set; }
[MaxLength(250)]
public string AboutMe { get; set; }
public Genders Gender { get; set; }
public virtual ICollection<Country> CountriesOfOperation
{
get { return this.countriesOfOperation; }
set { this.countriesOfOperation = value; }
}
}
這意味着,我必須創建註冊方法到帳戶控制器是這樣的:
[AllowAnonymous]
[Route("Clients/register")]
public async Task<IHttpActionResult> RegisterClient(ClientRegisterBindingModel model)
{
var user = new Client()
{
UserName = model.Email,
Email = model.Email,
RegistrationDate = DateTime.Now
};
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
return Ok();
}
同樣的事情去驅動類。當然,繼承重用像UserName,Email,PasswordHash等東西,但這也意味着我必須爲每種類型的用戶添加註冊之類的方法。這使得帳戶管理在屁股上很痛苦。
2)具有引用字段保持在單獨的表客戶端和驅動一個ApplicationUser
public class Client
{
public string FirstName { get; set; }
public ApplicationUser Account { get; set; }
}
這似乎有點好轉,但訪問客戶端我要像做clientInstance.Account.UserName
的用戶名,這仍然不夠優雅。
我對這項技術非常陌生,而且我還沒有探索過它的所有功能,所以我很可能錯過了一些相當明顯的東西。提前致謝。
保持客戶端和驅動在同一個表只是有不同的帳戶ID爲他們也許有一個叫'Group'和手動列創建它們擁有所有的客戶端爲第1組和所有驅動程序作爲第2組 – MethodMan
@MethodMan,止跌」把它們放在同一個表中留下太多未使用的列?例如,驅動程序類具有客戶端不具有的3-4個字段,這意味着它們對於客戶端行必須爲NULL。 – arnaudoff
如果你看到一個數據庫佈局的例子,你將會非常高興,你可以使用2個表格並構建一個連接查詢,我認爲你正在使得方法比需要的更困難。有很多方法來爲這個貓皮膚 – MethodMan