0

ApplicationUserがログインする必要があるビルトイン認証/認可システムを使用しています。一度認証されると、彼はSignInManagerでサインインしています。Asp.Net MVCは、ApplicationUserを拡張するカスタムユーザーを承認します

CustomUserApplicationUserのユーザーもいます。 CustomUserは、外部サービスを介して認証されます。彼が認証されると、私は彼が存在するかどうかをチェックし、そうでなければ彼を作り、CustomRoleを与えます。

CustomUserはどのようにして認証されますか?私は彼が許可されるべき行動の上に[Authorize(Roles="CustomRole")] 属性を置くことができるようにしたいと思います。それは可能ですか?私はその仕事をするために何をする必要がありますか?それとも良い方法がありますか?

EDIT

はここCustomUserの実装です。アプリケーションの下にあります。モデル

public class CustomUser : ApplicationUser 
{ 
    public int Gender 
    { 
     get; 
     set; 
    } 

    public string FCode 
    { 
     get; 
     set; 
    } 

    public bool Subscribed 
    { 
     get; 
     set; 
    } 
} 

これはCustomUserの簡略化されたバージョンです。

答えて

0

ためでしたか:

userManager.AddToRole(user.Id, "NameOfRole"); 

以下のような登録アクションを作成できます。 eredのユーザーは1つの役割から来ます。 Managerの他の役割を作成し、後でたとえば特定のユーザーを更新することができます。

using (var context = new ApplicationDbContext()) 
{ 
    if (ModelState.IsValid) 
    { 
     var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; 
     var result = await UserManager.CreateAsync(user, model.Password); 

     if (result.Succeeded) 
     { 
      await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); 
         //adding a role after register 
      var roleStore = new RoleStore<IdentityRole>(context); 
      var roleManager = new RoleManager<IdentityRole>(roleStore); 

      var userStore = new UserStore<ApplicationUser>(context); 
      var userManager = new UserManager<ApplicationUser>(userStore); 
      userManager.AddToRole(user.Id, "SimpleUser"); 
      // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
      // Send an email with this link 
      // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
      // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
      // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 

      return RedirectToAction("Index", "Home"); 
     } 
     AddErrors(result); 
    } 
} 

次に、承認属性でRolesを使用して承認することができます。

[Authorize(Roles="Manager, SimpleUser, so on..")] 
+0

問題はSignInManagerが拡張カスタムユーザーではなくApplicationUserのみを受け入れることです。私はCustomUserを作成して正しい役割に追加することができましたが、今は "彼にサインイン"することができなければなりません。それでSignInManager のインスタンスを取得して彼にサインインすることは可能ですか? – joks

+0

@joksはあなたのCustomUserが 'IdentityUser'によって実装されていますか? – Valkyrie

+0

@joksこの記事を見ると、カスタムユーザー用のモデルを作成する必要はありません.AppleUserを使用することができます。この記事のようにクラスに追加できるプロパティを追加したい場合は、http:// stackoverflow .com/questions/28335353/how-to-extend-available-of-user-identity-ID – Valkyrie

0

あなたは役割については、以下のような何かを行うことができる必要があります:

[Authorize(Roles = "CustomRole")] 
public ActionResult CustomRoleOnly() 
{ 
    return View(); 
} 

か、それはあなたが役割で許可したい場合は、使用してRolesを作成する必要があるユーザー

[Authorize(Users = "JoeBloggs, JaneDoe")] 
public ActionResult SpecificUserOnly() 
{ 
    return View(); 
} 
関連する問題