PDA

View Full Version : لاگین با اکانت گوگل



ali_72
پنج شنبه 01 مرداد 1394, 09:25 صبح
سلام
من این کد را برای ورود کاربر با اکانت گوگل خود نوشتم
ولی این خطا را توسط گوگل دریافت میکنم
Error: redirect_uri_mismatch
The redirect URI in the request: http://example.com/signin-google did not match a registered redirect URI.

در console.developers.google.com پروژه ای ایجاد کردم.آیتم Google+ API را فعال کردم. کلاینت آیدی را ایجاد کردم.در قسمت Authorized JavaScript Origins نام دامنه را قرار دادم. در بخش Authorized redirect URls آدرس http://example (http://<strong>example</strong>).com/signin-google را قرار دادم. GoogleClientId و GoogleClientSecretId را در وب کانفیگ پروژه قرار دادم.
و در قسمت Startup کد زیر را قرار دادم.

public partial class Startup
{
/// <summary>
/// این پراپرتی مقدار کلایت ای دی رو از وب دات کانفیگ در سازنده بدون ورودی در خودش ذخیره میکند
/// </summary>
public string GoogleClientId { get; set; }
/// <summary>
/// این پراپرتی مقدار کلایت سیکرت ای دی رو از وب دات کانفیگ در سازنده بدون ورودی در خودش ذخیره میکند
/// </summary>
public string GoogleClientSecretId { get; set; }


/// <summary>
/// سازنده بدون ورودی
/// به ازای هر بار نمونه سازی از کلاس، سازنده‌های بدون ورودی کلاس هر بار اجرا خواهند شد، توجه شود که می‌توان از
/// سازنده‌های استاتیک هم استفاده کرد، این سازنده فقط یک بار، در صورتی که از کلاس نمونه سازی شود ایجاد میگردد
/// </summary>
public Startup()
{
//Get Client ID from Web.Config
GoogleClientId = ConfigurationManager.AppSettings["GoogleClientId"];
//Get Client Secret ID from Web.Config
GoogleClientSecretId = ConfigurationManager.AppSettings["GoogleClientSecretId"];
}


///// <summary>
///// سازنده استاتیک کلاس
///// </summary>
//static Startup()
//{
//در صورتی که از این سازنده استفاده شود می‌بایست پراپرتی‌های تعریف شده در سطح کلاس به صورت استاتیک تعریف گردد تا
//بتوان در این سازنده سطح دسترسی گرفت
// GoogleClientId = ConfigurationManager.AppSettings["GoogleClientId"];
// GoogleClientSecretId = ConfigurationManager.AppSettings["GoogleClientSecretId"];
//}


// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Crea te);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.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
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationT ypes.ExternalCookie);


// Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
app.UseTwoFactorSignInCookie(DefaultAuthentication Types.TwoFactorCookie, TimeSpan.FromMinutes(5));


// Enables the application to remember the second login verification factor such as phone or email.
// Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
// This is similar to the RememberMe option when you log in.
app.UseTwoFactorRememberBrowserCookie(DefaultAuthe nticationTypes.TwoFactorRememberBrowserCookie);


// Uncomment the following lines to enable logging in with third party login providers
//app.UseMicrosoftAccountAuthentication(
// clientId: "",
// clientSecret: "");


//app.UseTwitterAuthentication(
// consumerKey: "",
// consumerSecret: "");


//app.UseFacebookAuthentication(
// appId: "",
// appSecret: "");


app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = GoogleClientId,
ClientSecret = GoogleClientSecretId,
CallbackPath = new PathString("/signin-google")
});
}
}


در RouteConfig کد زیر را اضافه کردم.

routes.MapRoute(name: "signin-google", url: "signin-google", defaults: new { controller = "Account", action = "ExternalLoginCallback" });





من تو نت خیلی گشتم. ولی نمیدونم کجا اشتباه کردم. ممنون میشم راهنمایی کنید.

TeacherMath
پنج شنبه 01 مرداد 1394, 09:43 صبح
به نظرم ایران توسط گوگل فعلا تحریمه به همین خاطر نمیشه از آن استفاده کرد.

ali_72
پنج شنبه 01 مرداد 1394, 09:58 صبح
به نظرم ایران توسط گوگل فعلا تحریمه به همین خاطر نمیشه از آن استفاده کرد.

فیلترشکن استفاده کردم
سرور سایت هم خارج از ایرانه