2017-04-11 68 views
2

我是OAuth的新用戶。有誰知道如何使用OAuth登錄,我已成功地產生通過將用戶名和密碼令牌,並在JavaScript中使用sessionStorage.setItem(「的accessToken」)來存儲令牌,使用OAuth登錄並從數據庫檢索用戶詳細信息

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#btnLogin').click(function() { 
      $.ajax({ 
       url: '/token', 
       method: 'POST', 
       contentType: 'application/json', 
       data: { 
        username: $('#txtEmail').val(), 
        password: $('#txtPassword').val(), 
        grant_type: 'password' 
       }, 
       success: function (response) { 

        sessionStorage.setItem('accessToken', response.access_token); 
        sessionStorage.setItem('userName', response.userName); 
        window.location.href = "Jobs.html"; 

       }, 
       error: function (jqXHR) { 
        $('#divErrorText').text(jqXHR.responseText); 
        $('#divError').show('fade'); 
       } 
      }); 
     }); 
    }); 

</script> 

這是我的自定義OAuthAuthorizationServerProvider

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
     var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>(); 

     //find a match with the entered username and password 
     AppUser user = await userManager.FindAsync(context.UserName, context.Password); 
     if(user == null) 
     { 
      context.SetError("Access Denied, Invalid Username or Password"); 
      return; 
     } 

     //responsible to fetch the authenticated user identity from the database and returns an object of type 「ClaimsIdentity」 
     ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, OAuthDefaults.AuthenticationType); 
     ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(userManager, CookieAuthenticationDefaults.AuthenticationType); 

     AuthenticationProperties properties = CreateProperties(user.UserName); 
     AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); 

     context.Validated(ticket); 
     context.Request.Context.Authentication.SignIn(cookieIdentity); 
    } 

我怎麼能登錄的用戶,我怎麼能檢索來自數據庫的用戶名和電子郵件,而無需使用sessionStorage.getItem。我需要爲此調用另一個LogIn API嗎? (像這樣)

public async Task<ActionResult> LogIn(LogInModel model) 
    { 
     if(!ModelState.IsValid) 
     { 
      return View(); 
     } 
userManager.FindAsync. 
     var user = await userManager.FindAsync(model.Email, model.Password); 

     // sign in the user using the cookie authentication middleware SignIn(identity) 
     if (user != null) 
     { 
      await SignIn(user); 
      return Redirect(GetRedirectUrl(model.ReturnUrl)); 
     } 

     ModelState.AddModelError("", "Kas Error Message -Inavlid email or password"); 
     return View(); 
    } 

private async Task SignIn(AppUser user) 
    { 
     var identity = await userManager.CreateIdentityAsync(
      user, DefaultAuthenticationTypes.ApplicationCookie); 

     GetAuthenticationManager().SignIn(identity); 
    } 

很抱歉,如果我聽起來厚:)任何幫助表示讚賞

回答

0

當你創建了一個Web API項目,並選擇使用ASP.Net身份應該已經生成一個包含AccountController類您需要的API調用,例如GetUserInfo()。看起來您必須執行單獨的調用才能獲取用戶詳細信息,但不確定是否有簡單的方法可以將調用進行登錄,然後將用戶詳細信息與訪問令牌結合起來。

相關問題