2013-09-30 7 views
45

क्षमा और इस सवाल के लिए अग्रिम धन्यवाद! मैं अभी भी एसओ के लिए नया हूँ।AspNet.Identity का उपयोग करके Asp.Net MVC5 RTM बिट्स के साथ किसी उपयोगकर्ता को लॉगिन/प्रमाणीकृत कैसे करें?

मैं MVC5, EF6, और वी.एस. 2013

का उपयोग कर एक वेब अनुप्रयोग पर काम कर रहा है मैं एक बार जारी की आर सी बिट्स के लिए कुछ समय के उन्नयन बिताया। वहां सभी महान पदों के लिए धन्यवाद: उदाहरण के लिए। Decoupling Microsoft.AspNet.Identity.* और Updating asp.net MVC from 5.0.0-beta2 to 5.0.0-rc1!

मेरे अनंत ज्ञान में, मैंने आरटीएम बिट्स पर जाने का फैसला किया कि @ हाओ कुंग ने यहां पोस्ट किया: How can I get early access to upcoming Asp.Net Identity changes?। मैंने सोचा कि मैं परेशानी को बचाऊंगा और जब हम अंततः आरटीएम बिल्ड प्राप्त करेंगे तो बहुत पीछे नहीं होंगे।

यह या तो एक दुःस्वप्न रहा है, या मैं पूरी तरह से कुछ खो रहा हूं (या दोनों) क्योंकि मैं आरसी 1 सामान के साथ काम कर रहे बुनियादी कार्यों को नहीं समझ सकता।

हालांकि ऐसा लगता है कि मैं नियंत्रक (Where is Microsoft.AspNet.Identity.Owin.AuthenticationManager in Asp.Net Identity RTM version?) के माध्यम से उपयोगकर्ता को लॉग इन कर रहा हूं ... मेरी विंडोज़ इडेंटिटी हमेशा खाली होती है और साइनइन कॉल करने के बाद प्रमाणित नहीं होती है। उपयोगकर्ता और दावे इवेंटेंट ऑब्जेक्ट सही ढंग से आबादी वाले हैं।

[HttpPost, AllowAnonymous, ValidateAntiForgeryToken] 
public virtual async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
{ 
    if (ModelState.IsValid) { 
     var userManager = new UserManager<EtdsUser>(new UserStore<EtdsUser>(new EtdsContext())); 
     var user = userManager.Find(model.UserName, model.Password); 
     if (user != null) { 
      var authenticationManager = HttpContext.GetOwinContext().Authentication; 
      authenticationManager.SignOut(new[] {DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.ExternalBearer}); 
      var claimsIdentity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); 
      authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = model.RememberMe}, claimsIdentity); 
      return RedirectToLocal(returnUrl); 
     } 
    } 
    ModelState.AddModelError("", "The user name or password provided is incorrect."); 
    return View(model); 
} 

(एक तरफ ध्यान दें पर::। मैं इस समय बाहरी उपयोगकर्ताओं में लॉग इन करने की जरूरत नहीं है)

यहाँ कार्रवाई विधि मैं बोल रहा हूँ (पूर्णता के लिए स्थानीय चर के लिए ले जाया गुण) है

कोई सुझाव? -or- क्या मुझे अपने सभी परिवर्तन वापस लेना चाहिए और बस वीएस 2013 आरटीएमडी तक प्रतीक्षा करें?


अद्यतन, इसे हाओ कुंग के मूल उत्तर के करीब बनाने के लिए कोड को दोहराया गया। हालांकि मैं अभी भी एक वैध उपयोगकर्ता पहचान के साथ खत्म नहीं होता है। मुझे लगता है कि मेरा प्रमाणीकरण प्रबंधक सही ढंग से असाइन नहीं किया गया है?

AuthenticationManger अब के रूप में परिभाषित किया गया है:

public IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } } 

SignInAsync अब एक अलग विधि है:

private async Task SignInAsync(EtdsUser user, bool isPersistent) 
{ 
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); 
    var claimsIdentity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); 
    AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent}, claimsIdentity); 
} 

"प्रस्थान करें" के बाद, डिबगर पता चलता है:

AuthenticationManager.User.Identity 
{System.Security.Principal.WindowsIdentity} 
    [System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity} 
    AuthenticationType: "" 
    IsAuthenticated: false 
    Name: "" 

"claimsIdentity "तब है:

claimsIdentity 
{System.Security.Claims.ClaimsIdentity} 
    Actor: null 
    AuthenticationType: "ApplicationCookie" 
    BootstrapContext: null 
    Claims: {System.Security.Claims.ClaimsIdentity.get_Claims} 
    IsAuthenticated: true 
    Label: null 
    Name: "alon" 
    NameClaimType: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name" 
    RoleClaimType: "http://schemas.microsoft.com/ws/2008/06/identity/claims/role" 

"साइन इन करें" कुछ भी नहीं बदलता है:

AuthenticationManager.User.Identity 
{System.Security.Principal.WindowsIdentity} 
    [System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity} 
    AuthenticationType: "" 
    IsAuthenticated: false 
    Name: "" 

फिर भी कोई प्रमाणीकरण है, लेकिन लगता है कि कोई त्रुटि फेंक दिया जाता है।


जैसा कि @ हाओ कुंग ने उत्तर दिया, StartUp.Auth बदल दिया।सीएस से:

var authOptions = new CookieAuthenticationOptions { ExpireTimeSpan = TimeSpan.FromHours(4.0)}; 
app.UseCookieAuthentication(authOptions); 

करने के लिए:

var authOptions = new CookieAuthenticationOptions { 
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
    LoginPath = new PathString("/Account/Login"), 
    ExpireTimeSpan = TimeSpan.FromHours(4.0) 
}; ... 
+0

एक इंटरफ़ेस है ... Microsoft.Owin.Security.IA प्रमाणीकरण प्रबंधक – user2315985

+0

HttpContext.GetOwinContext() नहीं है। प्रमाणीकरण IAuthenticationManager के कार्यान्वयन को वापस करता है? कम से कम मैंने सोचा कि यह किया है, लेकिन मुझे कुछ याद आ रही है। – ACG

उत्तर

36

तो यहाँ क्या लॉगिन मूल रूप से आरटीएम (ASPNET Identity sample code से नकल कोड) में तरह दिखेगा:

// 
    // POST: /Account/Login 
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = await UserManager.FindAsync(model.UserName, model.Password); 
      if (user != null) 
      { 
       await SignInAsync(user, model.RememberMe); 
       return RedirectToLocal(returnUrl); 
      } 
      else 
      { 
       ModelState.AddModelError("", "Invalid username or password."); 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

    private async Task SignInAsync(ApplicationUser user, bool isPersistent) 
    { 
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); 
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); 
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); 
    } 

संपादित करें: और आप की जरूरत अपने स्टार्टअप में परिवर्तनों का पालन करें .Auth.cs:

 app.UseCookieAuthentication(new CookieAuthenticationOptions { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/Account/Login") 
     }); 
+0

बहुत बढ़िया धन्यवाद ... बस उत्सुक है, "userManager" और "प्रमाणीकरण प्रबंधक" की मेरी तत्कालता सही दिखती है? मैंने अपनी पिछली पोस्ट के आधार पर अपनी लॉगिन विधि को संशोधित किया, इसलिए मुझे आशा है कि मैं सही रास्ते पर जा रहा हूं। – ACG

+0

मैंने अपनी लॉगिन विधि का पुन: उपयोग किया और सुझाव दिया है कि साइनइनएसिंक बनाया है। मेरा कोड अब आपके जैसा दिखता है (और आपके द्वारा बनाई गई मूल पोस्ट)। _userManager.FindAsync विधि मेरे सही कस्टम उपयोगकर्ता (EtdsUser) देता है। हालांकि प्रमाणीकरण प्रबंधक को कॉल करने के बाद। साइनइन ... उपयोगकर्ता। छवि अभी भी खाली है। आपके पिछले जवाब में एक समान प्रश्न के उत्तर में, आपने कहा "सबसे अधिक संभावना है कि साइन इन के साथ कुछ गलत हो गया है, और कोई दावा प्रिंसिपल नहीं है"। _userManager.CreateIdentityAsync एक वैध पहचान देता है, मैं वास्तव में इसे "हस्ताक्षरित" नहीं कर सकता। – ACG

+0

अतिरिक्त विवरण के साथ ऊपर अपना संपादित प्रश्न देखें। – ACG

संबंधित मुद्दे