2017-01-31 6 views
0

webpackを使用してhttp postを使用してユーザーログインを検証しようとしています。私は、ユーザー情報を渡すことができて、ユーザーは再びdbテーブルを検証しました。サーバーコードは正常に動作します。しかし、私は、サーバーからクライアントへの応答(オブジェクト)を渡す際につまずく。同様のコードはDotNet CoreのAngular2 SPA(ASP.NETコア1.0のchsakellクロスプラットフォームSPA、Angular 2 & TypeScriptから抽出および変更されたコード)で動作します。しかし、それはwebpackのAngular2でDotNet Coreで失敗します。誰かが問題の原因を特定するのを手伝ってくれることを願っています。それは、HTTP呼び出しが失敗した原因のでsystem.csWebpack Angular2はhttpポストから応答(オブジェクト)を受信しません

 system.cs 
            services.AddMvc() 
     .AddJsonOptions(opt => 
     { 
      var resolver = opt.SerializerSettings.ContractResolver; 
      if (resolver != null) 
      { 
       var res = resolver as DefaultContractResolver; 
       res.NamingStrategy = null; 
      } 
     }); 

System.cs 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Builder; 
using Microsoft.AspNetCore.Hosting; 
using Microsoft.AspNetCore.SpaServices.Webpack; 
using Microsoft.EntityFrameworkCore; 
using Microsoft.Extensions.Configuration; 
using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Logging; 
using Microsoft.Net.Http.Headers; 
using UCHICUDRS.Formatters; 
using System.Security.Claims; 
using UCHICUDRS.Models; 
using UCHICUDRS.Infrastructure.Repositories; 
using UCHICUDRS.Infrastructure.Services; 
using Newtonsoft.Json.Serialization; 
using UCHICUDRS.Infrastructure.Mappings; 

namespace UCHICUDRS 
{ 
    public class Startup 
    { 
     private static string _applicationPath = string.Empty; 
     private static string _contentRootPath = string.Empty; 

     public Startup(IHostingEnvironment env) 
     { 
      _applicationPath = env.WebRootPath; 
      _contentRootPath = env.ContentRootPath; 

      var builder = new ConfigurationBuilder() 
       .SetBasePath(env.ContentRootPath) 
       .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 
       .AddEnvironmentVariables(); 
      Configuration = builder.Build(); 
     } 

     public IConfigurationRoot Configuration { get; } 

     // This method gets called by the runtime. Use this method to add services to the container. 
     public void ConfigureServices(IServiceCollection services) 
     { 
      services.AddDbContext<UCHICUDRSContext>(options => 
       options.UseSqlServer(Configuration["Data:UCHICUDRSConnection:ConnectionString"])); 

      // Repositories    services.AddScoped<ILogRepository, LogRepository>(); 
      services.AddScoped<IRoleRepository, RoleRepository>();    services.AddScoped<IUserRepository, UserRepository>(); 
      services.AddScoped<IUserRequestRepository, UserRequestRepository>(); 
      services.AddScoped<IUserGroupRepository, UserGroupRepository>(); 
      services.AddScoped<IUserRoleRepository, UserRoleRepository>(); 

      // Services 
            services.AddScoped<IMembershipService, MembershipService>(); 
      services.AddScoped<IEncryptionService, EncryptionService>(); 

      services.AddAuthentication(); 

      // Polices 
      services.AddAuthorization(options => 
      { 
       // inline policies 
       options.AddPolicy("Administrator", policy => 
       { 
        policy.RequireClaim(ClaimTypes.Role, "Administrator"); 
       }); 

       options.AddPolicy("Manager", policy => 
       { 
        policy.RequireClaim(ClaimTypes.Role, "Manager"); 
       }); 

       options.AddPolicy("User", policy => 
       { 
        policy.RequireClaim(ClaimTypes.Role, "User"); 
       }); 

       options.AddPolicy("No Access", policy => 
       { 
        policy.RequireClaim(ClaimTypes.Role, " No Access"); 
       }); 
      }); 
/* 
      // Add MVC services to the services container 
      // Causing http header error 
            services.AddMvc() 
      .AddJsonOptions(opt => 
      { 
       var resolver = opt.SerializerSettings.ContractResolver; 
       if (resolver != null) 
       { 
        var res = resolver as DefaultContractResolver; 
        res.NamingStrategy = null; 
       } 
      }); 
*/ 
      // Add framework services. 
      services.AddMvc(); 
      services.AddMvc(options => 
      { 
       // The custom formatters need to be added to the MVC middleware, so that it knows how to handle media types 'text/csv'. 
       options.InputFormatters.Add(new CsvInputFormatter()); 
       options.OutputFormatters.Add(new CsvOutputFormatter()); 
       options.FormatterMappings.SetMediaTypeMappingForFormat("csv", MediaTypeHeaderValue.Parse("text/csv")); 
      }); 
     } 

     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
      loggerFactory.AddDebug(); 

      if (env.IsDevelopment()) 
      { 
       app.UseDeveloperExceptionPage(); 
       app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions { 
        HotModuleReplacement = true 
       }); 
      } 
      else 
      { 
       app.UseExceptionHandler("/Home/Error"); 
      } 

      AutoMapperConfiguration.Configure(); 

      app.UseCookieAuthentication(new CookieAuthenticationOptions 
      { 
       AutomaticAuthenticate = true, 
       AutomaticChallenge = true 
      }); 

      app.UseStaticFiles(); 

      app.UseMvc(routes => 
      { 
       routes.MapRoute(
        name: "default", 
        template: "{controller=Home}/{action=Index}/{id?}"); 

       routes.MapSpaFallbackRoute(
        name: "spa-fallback", 
        defaults: new { controller = "Home", action = "Index" }); 
      }); 
     } 
    } 
} 


operationResult.ts 

export class OperationResult { 
    Succeeded: boolean; 
    Message: string; 
    RetData: any; 
  
    constructor(succeeded: boolean, message: string, retData: any) { 
        this.Succeeded = succeeded; 
        this.Message = message; 
     this.RetData = retData; 
    } 
} 


    login.component.ts 
    import { Component, OnInit } from '@angular/core'; 
    import { ViewChild } from '@angular/core'; // for showModelMsg 
    import { Router, ActivatedRoute, RouterLink } from '@angular/router'; 
    // import {FORM_DIRECTIVES, REACTIVE_FORM_DIRECTIVES} from '@angular/forms'; 
    import {FormBuilder, FormGroup, Validators} from '@angular/forms'; 

    import { Login } from '../../../core/models/login'; 
    import { User } from '../../../core/models/user'; 
    import { Role } from '../../../core/models/role'; 
    import { OperationResult } from '../../../core/models/operationResult'; 
    import { MembershipService } from '../../../core/services/membership.service'; 
      
    @Component({ 
        selector: 'login', 
     providers: [MembershipService, NotificationService], 
        template: require('./login.component.html'), 
     styles: [require('./account.component.css')] 
    }) 
    export class LoginComponent implements OnInit { 
     private _user: Login = new Login('', '', false); 

     private notificationService: NotificationService; // alertify is no longer be supported so when it put inside the constructor, the html won't display 
     // private router: Router; 
     private route: ActivatedRoute; 

     constructor(private membershipService: MembershipService, private router: Router) { } 

     ngOnInit() { 
      this._user.Username = ''; 
      this._user.Password = ''; 
      this._user.RememberMe = false; 
     } 

        login(): void { 
            var _authenticationResult: OperationResult = new OperationResult(false, '', null); 
      var _creds: User; 
            this.membershipService.login(this._user) 
                .subscribe(res => { 
                    _authenticationResult.Succeeded = res.Succeeded; 
                    _authenticationResult.Message = res.Message; 
                    _authenticationResult.RetData = res.RetData; 
        _creds = _authenticationResult.RetData; 
                }, 
                error => console.error('Error: ' + error), // error from http call 
                () => { 
                    if (_authenticationResult.Succeeded) { 
                        this.notificationService.printSuccessMessage('Welcome back ' + this._user.Username + '!'); 
                        localStorage.setItem('user', JSON.stringify(_creds)); 
                        this.router.navigate(['home']); 
                    } 
                    else { 
         this.showModelMsg("Login Failed", _authenticationResult.Message); 
                    } 
                }); 
        }; 
    } 


    membership.service.ts 
    import { Http, Response, Request } from '@angular/http'; 
    import { Injectable } from '@angular/core'; 
    import { DataService } from './data.service'; 
    import { Login } from '../models/login'; 
    import { Registration } from '../models/registration'; 
    import { Role } from '../models/role'; 
    import { User } from '../models/user'; 
    import { Observable } from "rxjs/Observable"; 
      
    @Injectable() 
    export class MembershipService { 
        private _accountLoginAPI: string = 'api/account/authenticate/'; 
     private _res: Observable<any>; 
     public redirectUrl: string; // store the URL so we can redirect after loggin in 

        constructor(public accountService: DataService) { } 
      
      
        login(creds: Login) { 
           this.accountService.set(this._accountLoginAPI); 
      return this.accountService.post(JSON.stringify(creds)); 
        } 
    } 


    data.service.ts 
    import { Http, Response } from '@angular/http'; 
    import { Headers, RequestOptions } from '@angular/http'; 
    import { Injectable } from '@angular/core'; 
    // import { Observable } from 'rxjs/Observable'; 
    import 'rxjs/add/operator/catch'; 
    import { Observable } from 'rxjs/Rx'; 
    import { OperationResult } from '../models/operationResult'; 
      
    @Injectable() 
    export class DataService { 
      
        public _pageSize: number; 
        public _baseUri: string; 
     public _headers: Headers; 
     public _options: RequestOptions; 
      
        constructor(public http: Http) { 
       this.init(); 
        } 

     init() { 
      this._headers = new Headers({ 'content-type': 'application/json' }); 
      this._options = new RequestOptions({ headers: this._headers }); 
     } 
      
        post(data?: any, mapJson: boolean = true) { 
      var _authenticationResult: OperationResult = new OperationResult(false, '', null); 
            if (mapJson) { 
       var res = this.http.post(this._baseUri, data, this._options) 
            .map(response => <any>(<Response>response).json()); 
       return res; 
      } 
            else 
                return this.http.post(this._baseUri, data, this._options) 
        .map(response => response.json()) 
        .catch(this.handleError); 
        } 
    } 


AccountController.cs 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Mvc; 
using UCHICUDRS.Models; 
using UCHICUDRS.ViewModels; 
using UCHICUDRS.Infrastructure.Repositories; 
using UCHICUDRS.Infrastructure.Services; 
using UCHICUDRS.Infrastructure.Core; 
using System.Security.Claims; 
using Microsoft.AspNetCore.Authentication.Cookies; 
using AutoMapper; 


namespace UCHICUDRS.Controllers 
{ // https://chsakell.com/2016/01/01/cross-platform-single-page-applications-with-asp-net-5-angular-2-typescript/ 
    [Route("api/[controller]")] 
    public class AccountController : Controller 
    { 
     private readonly IMembershipService _membershipService; 
     private readonly IUserRepository _userRepository; 
     private readonly IRoleRepository _roleRepository; 
     private readonly ILogRepository _loggingRepository; 

     public AccountController(IMembershipService membershipService, 
      IUserRepository userRepository, 
      IRoleRepository roleRepository, 
      ILogRepository _errorRepository) 
     { 
      _membershipService = membershipService; 
      _userRepository = userRepository; 
      _roleRepository = roleRepository; 
      _loggingRepository = _errorRepository; 
     } 

     [HttpPost("authenticate")] 
     public async Task<IActionResult> Login([FromBody] Login user) 
     { 
      IActionResult _result = new ObjectResult(false); 
      GenericResult _authenticationResult = null; 
      Console.WriteLine("123"); 
      try 
      { 
       MembershipContext _userContext = _membershipService.ValidateUser(user.Username, user.Password); 

       if (_userContext.User != null) 
       { 
        IEnumerable<Role> _roles = _userRepository.GetRoles(user.Username); 
        List<Claim> _claims = new List<Claim>(); 
        foreach (Role role in _roles) 
        { 
         Claim _claim = new Claim(ClaimTypes.Role, role.Name, ClaimValueTypes.String, user.Username); 
         _claims.Add(_claim); 
        } 
        await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, 
         new ClaimsPrincipal(new ClaimsIdentity(_claims, CookieAuthenticationDefaults.AuthenticationScheme)), 
         new Microsoft.AspNetCore.Http.Authentication.AuthenticationProperties { IsPersistent = user.RememberMe }); 

        _authenticationResult = new GenericResult() 
        { 
         Succeeded = true, 
         Message = "Authentication succeeded", 
         RetData = _userContext.User 
        }; 
       } 
       else 
       { 
        _authenticationResult = new GenericResult() 
        { 
         Succeeded = false, 
         Message = "Username and Password don't match. Authentication failed.", 
         RetData = null 
        }; 
       } 
      } 
      catch (Exception ex) 
      { 
       _authenticationResult = new GenericResult() 
       { 
        Succeeded = false, 
        Message = ex.Message, 
        RetData = null 
       }; 

       _loggingRepository.Add(new Log() { Message = ex.Message, StackTrace = ex.StackTrace, RecIn = DateTime.Now }); 
       _loggingRepository.Commit(); 
      } 

      _result = new ObjectResult(_authenticationResult); 
      return _result; 
     } 
} 


GenericResult.cs 
namespace UCHICUDRS.Infrastructure.Core 
{ 
    public class GenericResult 
    { 
     public bool Succeeded { get; set; } 
     public string Message { get; set; } 
     public object RetData { get; set; } 
    } 
} 

答えて

0

コメントを外して、次のコードは、私はそれをコメントしています。次にhttpにhttpヘッダを手動で追加して動作させます。上記のコードのコメントを外していない場合、http post応答はクライアントにデータを戻します。コードは今でも動作しますが、理由はわかりません。

関連する問題