2017-07-26 48 views
1

在我的MVC網絡應用程序中,我使用ASP.NET身份和任何通過應用程序本身註冊的人,我希望強制執行我們在IdentityConfig中設置的密碼策略。然而,任何我們通過我們的外部應用程序之一註冊的人(不使用ExternalLoginCallback),我們希望允許他們通過他們在該應用程序中擁有的密碼進行傳遞,並且現在已在我們的系統中註冊。ASP.NET MVC身份以編程方式創建用戶繞過密碼驗證

搜索失敗了,我試圖覆蓋或寫我自己的方法,實現這個想知道是否有人知道通過身份達到這個好方法。

回答

0

當你與密碼登錄,你可能在你的AccountController一個登錄行動是這樣的:

var result = await SignInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, shouldLockout: true); 

如果你想只在用戶登錄,您可以使用此:

var result = await SignInManager.SignInAsync(user, true, true); 
+0

感謝您的回覆!對不起,如果它不明確,但我們還需要在身份登記,而不是讓他們通過。雖然我認爲這激發了我使用Create方法註冊它們時沒有密碼,然後只是每次循環回到我們的外部應用程序進行身份驗證。 – Ringo64

+0

@ Ringo64哦,我以爲這些用戶已經註冊了。您可以使用'await UserManager.CreateAsync(model);'以相同的方式註冊一個新用戶。 – Alisson

0

在你的外部回調中,你可以做的伎倆......下面的代碼是使用MVIN的OWIN安全認證實現的。

public async Task<ActionResult> ExternalLoginCallback(string returnUrl) 
{ 
    try 
    { 
     var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); 
     //get the user info i.e. username/email from claim and validate this user from identity. 
     //You can manually do it by fetching the data from identity tables. 
     //Match the case and generate a session and direct to home. 
    } 
    catch(Exception ex) 
    { 
    } 
} 

確保您已註冊的應用程序在startup.cs和獲取用戶信息,並把他們安置在從第三方授權服務器索賠。

//app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions() 
      //{ 
      // ClientId = "", 
      // ClientSecret = "" 
      // Claims here after thentication 
      //}); 
相關問題