我目前正在開發具有 ASP.NET Core (2.1) API 服務器/后端和 Angular 單頁應用程序前端的應用程序。對于用戶身份驗證,我使用 ASP.NET Core Identity 以及其中提供的幾種默認方法,AccountController用于登錄、注銷、注冊、忘記密碼等。我的 Angular 應用程序訪問此 API 以執行這些操作。Angular 客戶端文件直接托管wwwroot在 ASP.NET Core 應用程序之外。我在localhost:5000.身份驗證在 ASP.NET Core 的Startup類中配置如下:services.AddIdentity<ApplicationUser, IdentityRole>(config => { config.SignIn.RequireConfirmedEmail = false; config.Password.RequiredLength = 8; }) .AddEntityFrameworkStores<AppDbContext>() .AddDefaultTokenProviders();// Prevent API from redirecting to login or Access Denied screens (Angular handles these).services.ConfigureApplicationCookie(options =>{ options.Events.OnRedirectToLogin = context => { context.Response.StatusCode = 401; return Task.CompletedTask; }; options.Events.OnRedirectToAccessDenied = context => { context.Response.StatusCode = 403; return Task.CompletedTask; };});注冊和忘記密碼都可以順利運行。調用 API 并采取適當的操作。登錄似乎有效:該方法在 Angular 中調用:@Injectable()export class AuthService extends BaseService { constructor(private httpClient: HttpClient) { super(httpClient, `${config.SERVER_API_URL}/account`); } // Snip login(login: Login): Observable<boolean> { return this.httpClient.post<boolean>(`${this.actionUrl}/Login`, login) .catch(this.handleError); } // Snip}
- 3 回答
- 0 關注
- 322 瀏覽
添加回答
舉報
0/150
提交
取消