2016-04-04 11 views
2

私は「Remember Me」機能を私のWeb Apiプロジェクトに実装しようとしています。ASP.NET Web APIクッキーを使用した「Remember Me」機能

私はしたいと思います:

  • はときでユーザーログインミー機能を覚えています。常にログインしているユーザーを維持するために、彼らはウェブサイトを訪問する際、ユーザは必要はユーザ名パスワードごとに単一の時間を入力しないよう
  • は、クッキーを保存します。
  • 最後のログイン時に保存されたのクッキーを読んでユーザーにサインインします。私が考えてい

もう一つの質問は...私は、ユーザーがが私を忘れないでくださいチェックボックスをチェックするとJavaScriptを使用してクッキーを生成しようとしています。これは可能ですか?

OR

私はAccountControllerRememberMe()を実装する必要があります?

追加: ここに私のコードはApplicationOAuthProviderです。

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

     ApplicationUser user = await userManager.FindByNameAsync(context.UserName); 

     if (user == null) {...} 

     if (userManager.IsLockedOut(user.Id)) {...} 

     if (!(await userManager.CheckPasswordAsync(user, context.Password))) 
     { ... } 

     if (!user.EmailConfirmed) {...} 

     ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, 
      OAuthDefaults.AuthenticationType); 
     ClaimsIdentity cookiesIdentity = 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(cookiesIdentity); 

JavaScriptでご覧ください。

$('#checkbox').click(function() { 

    if ($('#checkbox').is(':checked')) { 
     // save username and password 
     username = $('#txtLoginEmail').val(); 
     password = $('#pass').val(); 
     checkbox = $('#chkRememberMe').val(); 
    } else { 
     username = ''; 
     password = ''; 
     checkbox = ''; 
    } 
}); 

答えて

2

は、あなたがこの機能を提供できるようにするアプリでリフレッシュトークンを実装する必要があります。

基本的には、リフレッシュトークンを生成するRefreshTokenOAuthProviderを作成する必要があります。 client_idの2つのタイプを使用して、覚えが必要なクライアント間で違いを生むことができます。

this excellent series of blog postsで説明されています(ちょっと古いと思われるかもしれませんが、オーリンセットアップに関する情報は金です)。

+0

あなたのリンクは間違っていると思います。 [This](http://bitoftech.net/2014/07/16/enable-oauth-refresh-tokens-angularjs-app-using-asp-net-web-api-2-owin/)は正しいものです – Pikoh

関連する問題