AuthenticationResult IAuthenticationClient.VerifyAuthentication(HttpContextBase context) { try { Context db = new Context(); string code=context.Request["code"]; AccessTokenDB token = new AccessTokenDB(); var address = String.Format("https://oauth.vk.com/access_token?client_id={0}&client_secret={1}&code={2}&redirect_uri={3}",this.appId,this.appSecret,code,this.redirectUri); WebClient client = new WebClient(); client.Encoding = System.Text.Encoding.UTF8; Person person; var response = client.DownloadString(address); var access_token=JsonConvert.DeserializeObject<AccessTokenAndId>(response); token.AccessToken = access_token.accessToken; db.AccessToken.Add(token); db.SaveChanges(); address = String.Format("https://api.vk.com/method/users.get?uids={0}&fields=nickname", access_token.userId); client.Encoding = System.Text.Encoding.UTF8; response = client.DownloadString(address); person = JsonConvert.DeserializeObject<Persons>(response).People[0]; return new AuthenticationResult(true,(this as IAuthenticationClient).ProviderName,access_token.userId,person.FirstName + " " +person.LastName,new Dictionary<string,string>()); } catch(Exception ex){ return new AuthenticationResult(ex); } }
public SimpleMembershipInitializer() { Database.SetInitializer<Context>(null); try { using (var context = new Context()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } }
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { return RedirectToAction("Manage"); } if (ModelState.IsValid) { // Добавление нового пользователя в базу данных using (Context db = new Context()) { UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); // Проверка наличия пользователя в базе данных if (user == null) { // Добавление имени в таблицу профиля db.UserProfiles.Add(new UserProfile { UserName = model.UserName }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("UserName", "Имя пользователя уже существует. Введите другое имя пользователя."); } } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return View(model); }