設置: 僅使用Web API的新MVC5項目。新增了Facebook AppId和Secret。帶有Facebook訪問令牌的MVC 5 Web API無需Cookie Cookie即可訪問RegisterExternal
通過傳入用戶名和密碼,我可以從Token
端點獲得我的Web API的令牌。然後使用該標記進行進一步調用。
但
我想與Facebook SDK的iOS應用程序的幫助下,註冊新用戶。 我正在使用Facebook SDK獲取訪問令牌。 (假設在這一點上,我有一個訪問令牌)。
我知道的下一件事是通過在Authorization
標頭中傳遞此令牌來調用api/Account/RegisterExternal
端點Bearer [Access Token]
但這會導致500服務器錯誤。
我想我知道原因,Cookie丟失了。我用來自Fidler的cookie進行了同樣的調用,並且工作正常。 (通過去ExternalLogins
端點提供的URL接收Cookie)。 由於Cookie中缺少await Authentication.GetExternalLoginInfoAsync();
,因此RegisterExternal操作返回null。
// POST api/Account/RegisterExternal
[OverrideAuthentication]
[HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
[Route("RegisterExternal")]
public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var info = await Authentication.GetExternalLoginInfoAsync();
if (info == null)
{
return InternalServerError();
}
var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
IdentityResult result = await UserManager.CreateAsync(user);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
result = await UserManager.AddLoginAsync(user.Id, info.Login);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
return Ok();
}
我不想讓3個電話到我的Web API,要求外部登錄,然後轉到該URL,並在Web瀏覽器爲Facebook訪問令牌身份驗證,然後調用RegisterExternal端點與訪問令牌和我需要收集這些電話之間的Cookie。
正如我所說我沒有改變任何模板,除了Facebook的ID。代碼仍然如下。
public partial class Startup
{
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
public static string PublicClientId { get; private set; }
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
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),
AllowInsecureHttp = true
};
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);
app.UseFacebookAuthentication(
appId: "xxxxxxxxxxxxxxx",
appSecret: "xxxxxxxxxxxxxxxxxxxxxxxx");
}
}
據我知道的Web API不需要Cookie和時,我有本地令牌從Token
端點出現真實的,但爲什麼它需要的Cookie在第一時間做ExternalRegister
WebApiConfig類看起來是這樣的,當並且不應該config.SuppressDefaultHostAuthentication();
避免任何Cookie的需要
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
如果我錯過了點這裏..我的意圖是不需要使用Web瀏覽器在原生iOS應用的令牌,我不知道。即Facebook SDK獲取訪問令牌並使用該呼叫RegisterExternal
獲取本地令牌並創建該用戶身份。
我做了我的功課,我被卡在這個想法。 感謝讚賞!
找到任何解決方案嗎?我會很感興趣,因爲我有同樣的問題。 – Freddy
@Freddy是的,我結束了將不同的作品放在一起來創建我自己的解決方案,效果很好。將張貼它。目前出城,如果我沒有發佈,請在4天后給我打電話。 – CodetrixStudio
這將是真正慷慨的你。我目前正在創建我自己的解決方案... – Freddy