2016-10-03 12 views
0

私はASP.NET WEB APIの複数のクライアントアプリケーションを開発中です。最初のクライアントはユーザー名とパスワードで認証し、2番目の認証はコード(文字列タイプ)で認証します。複数のプロバイダweb api

同じアプリで複数のプロバイダを使用することはできますか?私はまた、コードを確認

public void ConfigureAuth(IAppBuilder app) 
{ 
     app.CreatePerOwinContext(ApplicationDbContext.Create); 
     app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
     app.UseCookieAuthentication(new CookieAuthenticationOptions()); 
     app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

     // Configure the application for OAuth based flow 
     PublicClientId = "self"; 

      OAuthOptions = new OAuthAuthorizationServerOptions 
     { 
      TokenEndpointPath = new PathString("/Token"), 
      Provider = new ApplicationOAuthProvider(PublicClientId), 

      AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"), 
      AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), 
      // In production mode set AllowInsecureHttp = false 
      AllowInsecureHttp = true 
     }; 
     app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 

} 

私ApplicationOAuthProviderクラスではなく、最初のアプリは、コードを使用していません: は、ここでは、コードです。

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
{ 
    var userManager = context.OwinContext.GetUserManager <ApplicationUserManager>(); 
    ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password); 
    ApplicationUser userByName = await userManager.FindByNameAsync(context.UserName); 

    var data = await context.Request.ReadFormAsync(); 
    var code = data["code"]; 

    if (userByName == null || userByName.Code != code) 
    { 
     context.SetError("invalid_grant", "The user name or password is incorrect."); 
     return; 
    } 


    ClaimsIdentity oAuthIdentity = await userByName.GenerateUserIdentityAsync(userManager,     OAuthDefaults.AuthenticationType); 
    ClaimsIdentity cookiesIdentity = await userByNameCristina.GenerateUserIdentityAsync(userManager, 
    CookieAuthenticationDefaults.AuthenticationType); 

    AuthenticationProperties properties = CreateProperties(userByNameCristina.UserName,data["code"]); 
    AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); 
    context.Validated(ticket); 
    context.Request.Context.Authentication.SignIn(cookiesIdentity); 
} 

答えて

0

私はOAuthを有効にしており、カスタムのCookieベースのソリューションを持っています。工場

public class WebLoginAuthenticationMiddleware : AuthenticationMiddleware<WebLoginAuthenticationOptions> 
{ 
    public WebLoginAuthenticationMiddleware(OwinMiddleware nextMiddleware, 
              WebLoginAuthenticationOptions authOptions) 
     : base(nextMiddleware, authOptions) 
    { 

    } 

    protected override AuthenticationHandler<WebLoginAuthenticationOptions> CreateHandler() 
    { 
     return new WebLoginAuthenticationHandler(); 
    } 
} 

AuthenticationHandler

internal class WebLoginAuthenticationHandler : AuthenticationHandler<WebLoginAuthenticationOptions> 
{ 
    protected override async Task<AuthenticationTicket> AuthenticateCoreAsync() 
    { 
     await Task.Yield(); 

     var cookie = Context.Request.Cookies[config.CookieName]; 

     // Return unauthorized if no cookie exists. 
     if (cookie == null) 
      return null; 

     //Check authentication 
     // do stuff... 

     //User is authenticated - cookie match found 
     var authenticationProperties = CreateAuthenticationProperties(session); 

     var identity = CreateIdentity(buildings, session); 
     return new AuthenticationTicket(identity, authenticationProperties); 
    } 

    private static AuthenticationProperties CreateAuthenticationProperties() 
    { 
     return new AuthenticationProperties 
     { 
      IssuedUtc = DateTime.UtcNow, 
      ExpiresUtc = DateTime.UtcNow.AddHours(12), 
      AllowRefresh = true, 
      IsPersistent = true 
     }; 
    } 

    private ClaimsIdentity CreateIdentity() 
    { 
     var identity = new ClaimsIdentity(Options.AuthenticationType); 
     // add claims 
     return identity; 
    } 
} 
として

ウェブアピコンフィグ

app.UseWebLoginAuthentication(Container); // Custom cookie solution 
    ConfigureIdentityManager(app); // Identify manager 
    ConfigureAuth(app); // OAuth provider 

AuthenticationMiddlewareを

関連する問題