Windows身份验证和Angular 4应用程序
我正在编写一个Angular 4应用程序,它需要从Asp.NETWebAPI获取一些数据。我们正在为WebAPI使用windows身份验证,我想知道如何将用户的windows身份从我的Angular应用程序传递到WebAPI。我发现了两个例子,它们涉及到用MVC应用程序嵌套您的应用程序,但我想让UI远离MVC。有没有一种方法可以在不将.net mvc添加到我的Angular网站的情况下执行此操作?当您将http请求从Angular发送到您需要使用的WebAPI时 RequestOptions({withCredentials=true}) 下面是一个调用api的示例安全服务Windows身份验证和Angular 4应用程序,angular,asp.net-web-api,windows-authentication,Angular,Asp.net Web Api,Windows Authentication,我正在编写一个Angular 4应用程序,它需要从Asp.NETWebAPI获取一些数据。我们正在为WebAPI使用windows身份验证,我想知道如何将用户的windows身份从我的Angular应用程序传递到WebAPI。我发现了两个例子,它们涉及到用MVC应用程序嵌套您的应用程序,但我想让UI远离MVC。有没有一种方法可以在不将.net mvc添加到我的Angular网站的情况下执行此操作?当您将http请求从Angular发送到您需要使用的WebAPI时 RequestOptions({
@Injectable()
export class SecurityService {
private baseUrl = 'http://localhost:64706/api/security/';
private auth: Auth;
private options = new RequestOptions({ withCredentials: true });
constructor(private http: Http) {
console.log('Creating Service');
console.log('Service Pre Http');
this.http.get(this.baseUrl, this.options)
.map((res) => this.extractData<Auth>(res))
.subscribe(newItem => {
console.log('Service Subscribe');
this.auth = newItem;
})
}
public isUser(): Observable<boolean> | boolean {
if (!this.auth) {
return this.http.get(this.baseUrl, this.options)
.map(res => {
this.auth = this.extractData<Auth>(res);
return this.auth.isUser;
});
}
else {
return this.auth.isUser;
}
}
private extractData<T>(res: Response) {
if (res.status < 200 || res.status >= 300) {
throw new Error('Bad response status: ' + res.status);
}
const body = res.json ? res.json() : null;
return <T>(body || {});
}
}
如果您使用的是.net Core,那么您现在可以在WebAPI控制器中访问
this.User.Identity.IsAuthenticated
NB:如果您使用的是ASP.Net Core 2.0,则需要遵循此处的“Windows身份验证(HTTP.sys/IISIntegration)”部分
您还必须记住在主机上启用Windows身份验证,例如IIS或IISExpress
您可能需要启用CORS。此处的文档很好:
如果您在“飞行前检查”中遇到错误,那么您还需要启用匿名访问也许该链接将对您有所帮助:从我所看到的情况来看,他们正在使用常规身份验证。我想使用WindowsAuthentication@AlexanderM这个问题回答得对吗?如果是,请标出答案。您在回答中说“IIS或IISExpress”。你试过IISExpress了吗?因为我有,我不能让它工作是的,两者都使用铬。哪些浏览器尝试过?只有Internet Explorer。这是需要支持的主浏览器。我将尝试Chrome,看看它是否适合使用RequestOptions,您需要从'@angular/http'导入{RequestOptions}代码>。
export class Auth {
isAdmin: boolean;
isUser: boolean;
}