我有两个项目,一个是WEB API项目,另一个是SPA项目.
这是我到目前为止所做的:
我创建了OWIN Startup类:
[assembly: OwinStartup(typeof(CodeArt.WebApi.App_Start.Startup))] namespace CodeArt.WebApi.App_Start { public class Startup { static Startup() { PublicClientId = "self"; UserManagerFactory = () => new UserManager<UserModel>(new UserStore<UserModel>()); OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/Token"),Provider = new OAuthAuthorizatonServer(PublicClientId,UserManagerFactory),AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),AllowInsecureHttp = true }; } public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; } public static Func<UserManager<UserModel>> UserManagerFactory { get; set; } public static string PublicClientId { get; private set; } public void Configuration(IAppBuilder app) { ConfigureAuth(app); } public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions()); app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalBearer); app.USEOAuthBearerTokens(OAuthOptions); } }
我已将Web API配置为仅使用承载令牌身份验证:
private static void ConfigureBearerTokenAuthentication(HttpConfiguration config) { config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(Startup.OAuthOptions.AuthenticationType)); }
我已经配置了WEB API来支持CORS:
private static void ConfigureCrossOriginResourseSharing(HttpConfiguration config) { var cors = new EnableCorsAttribute("*","*","*"); config.EnableCors(cors); }
我创建了OAuthAuthorizationServerProvider类.从这个类我只设法让我的代码调用这个方法:
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) { if(context.ClientId == null) { context.Validated(); } return Task.FromResult<object>(null); }
内部的if条件总是被执行.
在我的水疗项目中,我有以下内容:
这是我的viewmodel:
var vm = { grant_type: "password",userName: ko.observable(),password: ko.observable() };
var http = { post:function(url,data) { return $.ajax({ url: url,data: data,type: 'POST',contentType: 'application/json',dataType: 'jsonp' }); } } function loginClick() { var model = ko.mapping.toJS(vm.loginModel); var rez = $.param(model); http.post("http://localhost:3439/Token",rez) .done(function (data) { console.log(data); }) .fail(function(eror,stuff,otherstuff) { console.log(eror); console.log(stuff); console.log(otherstuff); }); }
我设置帖子的第一次尝试调用dataType为json,我得到了这个错误:
OPTIONS …:3439/Token 400 (Bad Request) jquery.js:7845
OPTIONS …:3439/Token No ‘Access-Control-Allow-Origin’
header is present on the requested resource. Origin
‘…:3304’ is therefore not allowed access.
jquery.js:7845XMLHttpRequest cannot load …3439/Token. No
‘Access-Control-Allow-Origin’ header is present on the requested
resource. Origin ‘…3304’ is therefore not allowed
access.
3个点代表http:// localhost.
第二次arround我将它的数据类型设置为jsonp,我得到了一个错误,指出不支持的“unsupported_grant_type”.
这两个调用都使它成为我上面提到的ValidateClientAuthentication,但它们都作为失败的请求发回.
现在我猜测问题与我如何发送数据而不是grand_type更相关,因为Visual Studion中的SPA模板设置为grant_type的授权类型:“密码”就像我做的那样.
另外我已经读过我必须序列化数据而不是在json中发送它才能使这个工作在这里是得到的确切的json序列化数据:
“grant_type =密码&安培;用户名= aleczandru&安培;密码= happynewYear&安培;的moduleId =型号/ appPostModels / loginModel”
Durandal Framework将模型ID属性设置为我的SPA模板中的所有对象.
任何人都能告诉我我做错了什么我过去两天一直试图解决这个问题吗?