D
D
Dmitry Filandor2017-11-19 13:29:34
ASP.NET
Dmitry Filandor, 2017-11-19 13:29:34

How to login from VK in ASP MVC5?

Hello! I use ASP MVC5, AspNet.Identity and I need to log in from VK using OAUTH
While I'm trying locally (express iis)
Here's what I did:
1) Created a "web site" application in VK, Open API - enabled, Site address: http://localhost :1729; Base domain : localhost; Trusted redirect URI: - empty, because if anything to fill will throw an error;
2) Installed the Owin.Security.Providers.VKontakte package ( https://github.com/TerribleDev/OwinOAuthProviders)
3) Specified in Startup.Auth.cs:
app.UseVKontakteAuthentication("372323899", "vdQOYlASDfsagNhBr0vOAUX");
4) I press - joxi.ru/YmEkNxCZ7pJ7A6 seconds 20 does something and all the same window, I look at the console -joxi.ru/zAN5kgIlLY6W29
When the debug went through, everything seems to work, it receives a token, but probably due to the fact that the Trusted redirect URI is not configured, it does not translate where necessary, well, I want to take information about the user's login and email and create an account for him on website .... Well, it doesn’t translate to the VK page in order to ask - "grant access to the application" ....
Guys help me, double beer from me
-------
UP I
see an access error dl3.joxi on the network tab .net/drive/2017/11/22/0000/0020/4116/16/f3... maybe he doesn’t want to from the locale, I’ll try to throw it on the server

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Filandor, 2017-12-04
@LifeAct

That's it, the task is solved, with the help of https://github.com/DukeNuken/Duke.Owin.VkontakteMi... everything is done with a little kick:
Add nuget package - search for "Duke.Owin.VkontakteMiddleware"
Add module in Startup.Auth.cs of your mvc 5 project
app.UseVkontakteAuthentication("{AppId}", "{AppSecret}", "{PERMISSIONS}");
{PERMISSIONS} - it is the comma-separated string. For example "email,audio" More info here vk.com/dev/permissions

// GET: /Account/ExternalLoginCallback
        [AllowAnonymous]
        public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
        {
            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
            if (loginInfo == null)
            {
                return RedirectToAction("Login");
            }

            // Sign in the user with this external login provider if the user already has a login
            var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
            switch (result)
            {
                case SignInStatus.Success:
                    return RedirectToLocal(returnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
                case SignInStatus.Failure:
                default:
                    // If the user does not have an account, then prompt the user to create an account
                    ViewBag.ReturnUrl = returnUrl;
                    ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                    return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.DefaultUserName });
            }
        }

The only thing that pops up is this error - https://github.com/DukeNuken/Duke.Owin.VkontakteMi... although it is not particularly significant for me, I immediately register a local account according to the data received. But it would be nice to decide

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question