2
我已經完成了使用Oauth 1.0的MVC網站1.0
當我嘗試使用本地主機的Google帳戶(從調試模式)進行授權時,我流利地進行授權沒有任何問題,但是當我在服務器上發佈我的網站時,一些問題,當我點擊按鈕「谷歌登錄」我得到錯誤。請參閱下面的屏幕。當我嘗試谷歌授權獲取錯誤:invalid_request網站發佈時出錯。 Oauth 1.0
開發人員請幫我解決這個問題。坦克你
P.S. 192.168.77.155 - 這是我的內部服務器IP,但我無法想象爲什麼要顯示它。
返回信息野兔:
internal class ExternalLoginResult : ActionResult
{
public ExternalLoginResult(string provider, string returnUrl)
{
Provider = provider;
ReturnUrl = returnUrl;
}
public string Provider { get; private set; }
public string ReturnUrl { get; private set; }
public override void ExecuteResult(ControllerContext context)
{
OAuthWebSecurity.RequestAuthentication(Provider, ReturnUrl);
}
}
public ActionResult ExternalLoginCallback(string returnUrl)
{
GooglePlusClient.RewriteRequest();
var result = OAuthWebSecurity.VerifyAuthentication();
if (result.IsSuccessful)
{
ProfilePicture helper = new ProfilePicture();
// name of the provider we just used
OauthProvider provider = helper.GetProvider(result.Provider);
if ((int)provider == 0)
{
Logger.Fatal("Unknown Oauth Provider try to SignIn. Check Providers Name (maybe it changeed)");
return null; //todo MessageBox for Unkown Provider, or something wrong
}
// provider's unique ID for the user
var uniqueUserID = result.ProviderUserId;
// since we might use multiple identity providers, then
// our app uniquely identifies the user by combination of
// provider name and provider user id
var uniqueID = provider + "/" + uniqueUserID;
// we then log the user into our application
// we could have done a database lookup for a
// more user-friendly username for our app
FormsAuthentication.SetAuthCookie(uniqueID, false);
string userName;
string nameAndLsatName = string.Empty;
var userDataFromProvider = result.ExtraData;
if (provider.Equals(OauthProvider.Twitter))
{
userName = result.UserName;
}
else
{
userName = userDataFromProvider["username"];
nameAndLsatName = userDataFromProvider["name"];
}
//Check if user already is in Db with Provider
var chekUserName = Uow.Users.Data.Where(x => x.UserName == userName && x.UserGroup.Id == (int)provider).FirstOrDefault();
if (chekUserName == null)
{
MM.Data.Model.User user = new MM.Data.Model.User();
user.UserName = userName;
if (!provider.Equals(OauthProvider.Twitter))
{
user.FirstName = nameAndLsatName.Split(' ')[0];
user.LastName = nameAndLsatName.Split(' ')[1];
}
user.Email = userName; //it'a Email
if (provider.Equals(OauthProvider.Twitter))
{
user.ShowNameAndLastName = false;
}
else
{
user.ShowNameAndLastName = true;
}
user.GroupId = (int)provider;
if (provider.Equals(OauthProvider.Twitter))
{
user.ProfilePicture = helper.GetImageInBytesByProvider(provider, userName);
}
else
{
user.ProfilePicture = helper.GetImageInBytesByProvider(provider, uniqueUserID);
}
Uow.Users.Add(user);
Uow.SaveChanges();
}
//Valid Login
//todo need improvement
var userModel = Uow.Users.GetSingle(x => x.UserName == userName && x.UserGroup.Id == (int)provider);
Session["User"] = new LoggedUserModel
{
Id = userModel.Id,
UserName = userName,
ProfilePicture = userModel.ProfilePicture
};
Session["UserId"] = userModel.Id;
//FormsAuthentication.SetAuthCookie(useruserNamename, false);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
return RedirectToAction("Index", "Home");
// return View("", result);
}
return null; //need change
}
我的網站不使用我的服務器內部IP不通(192.168.77.155)它不是公網IP。 –
請將您用於重定向到Google Screen的代碼發佈 – Rajat
我認爲這不是代碼有罪,因爲在Localhost中一切正常。但是我已經更新了問題並請參閱 –